The introduction documents dedicate many paragraphs to explaining the difference between new()
and make()
, but in practice, you can create objects within local scope and return them.
Why would you use the (frankly silly) pair of allocators?
The introduction documents dedicate many paragraphs to explaining the difference between new()
and make()
, but in practice, you can create objects within local scope and return them.
Why would you use the (frankly silly) pair of allocators?
Apart from everything explained in Effective Go, The main difference between
new(T)
and&T{}
is that the latter explicitly performs a heap allocation. However it should be noted that this is implementation dependent and thus may be subject to change.Comparing
make
tonew
makes little sense as the two perform entirely different functions. But this is explained in detail in the linked article.new(T) : it returns a pointer to type T a value of type *T, it allocates and zeroes the memory. new(T) is equivalent to &T{}.
make(T) : it returns an initialized value of type T, It allocates and initializes the memory. Its used for slices, map and channels.
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:
Go has multiple ways of memory allocation and value initialization:
&T{...}
,&someLocalVar
,new
,make
Allocation can also happen when creating composite literals.
new
can be used to allocate values such as integers,&int
is illegal:The difference between
new
andmake
can be seen by looking at the following example:Suppose Go does not have
new
andmake
, but it has the built-in functionNEW
. Then the example code would look like this:The
*
would be mandatory, so:Yes, merging
new
andmake
into a single built-in function is possible. However, it is probable that a single built-in function would lead to more confusion among new Go programmers than having two built-in functions.Considering all of the above points, it appears more appropriate for
new
andmake
to remain separate.Things you can do with
make
that you can't do any other way:It's a little harder to justify
new
. The main thing it makes easier is creating pointers to non-composite types. The two functions below are equivalent. One's just a little more concise:You need
make()
to create channels and maps (and slices, but those can be created from arrays too). There's no alternative way to make those, so you can't removemake()
from your lexicon.As for
new()
, I don't know of any reason offhand why you need it when you can use struct syntax. It does have a unique semantic meaning though, which is "create and return a struct with all fields initialized to their zero value", which can be useful.