Why would I make() or new()?

前端 未结 7 1967
面向向阳花
面向向阳花 2020-11-29 14:27

The introduction documents dedicate many paragraphs to explaining the difference between new() and make(), but in practice, you can create objects

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 15:22

    make function allocates and initializes an object of type slice, map, or chan only. Like new, the first argument is a type. But, it can also take a second argument, the size. Unlike new, make’s return type is the same as the type of its argument, not a pointer to it. And the allocated value is initialized (not set to zero value like in new). The reason is that slice, map and chan are data structures. They need to be initialized, otherwise they won't be usable. This is the reason new() and make() need to be different.

    The following examples from Effective Go make it very clear:

    p *[]int = new([]int) // *p = nil, which makes p useless
    v []int = make([]int, 100) // creates v structure that has pointer to an array, length field, and capacity field. So, v is immediately usable
    

提交回复
热议问题