New or Make
Go has multiple ways of memory allocation and value initialization:
&T{...}
, &someLocalVar
, new
, make
Allocation can also happen when creating composite literals.
new
new
can be used to allocate values such as integers, &int
is illegal:
make
It creates slices, maps, and channels only, and it returns an initialized (not zeroed) value of type T (not *T).
The reason for the distinction is that these three types represent, under the covers, references to data structures that must be initialized before use. A slice, for example, is a three-item descriptor containing a pointer to the data (inside an array), the length, and the capacity, and until those items are initialized, the slice is nil. For slices, maps, and channels, make initializes the internal data structure and prepares the value for use.
For instance,
allocates an array of 100 ints and then creates a slice structure with length 10 and a capacity of 100 pointing at the first 10 elements of the array. (When making a slice, the capacity can be omitted; see the section on slices for more information.) In contrast, new([]int) returns a pointer to a newly allocated, zeroed slice structure, that is, a pointer to a nil slice value.
new
V.S. make
The difference between new
and make
can be seen by looking at the following examples:
Example 1:
Example 2:
Suppose Go does not have new
and make
, but it has the built-in function NEW
. Then the example code would look like this:
The *
would be mandatory, so:
Yes, merging new
and make
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
and make
to remain separate.