Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between new and make?

Tags:

go

New does not initialize the memory, it only zeros it. It returns a pointer to a newly allocated zero value.

Make creates slices, maps, and channels only, and it returns them initialized.

What does "initialized" mean in this context? What other differences are there between new and make?

like image 450
wizztjh Avatar asked Aug 18 '14 07:08

wizztjh


People also ask

What is new and make in Golang?

The new function in Golang allocates memory but does not initialize it as the make function does. new returns a pointer to the newly allocated zeroed value of type T .

What is make in go?

Golang make() is a built-in slice function used to create a slice. The make() function takes three arguments: type, length, and capacity, and returns the slice. To create dynamically sized arrays, use the make() function. The make() function allocates a zeroed array and returns a slice that refers to that array.

Should I use make in Golang?

You need make() to create channels and maps (and slices, but those can be created from arrays too).

What is the difference between Slice and array in Golang?

Slices in Go and Golang The basic difference between a slice and an array is that a slice is a reference to a contiguous segment of an array. Unlike an array, which is a value-type, slice is a reference type. A slice can be a complete array or a part of an array, indicated by the start and end index.


2 Answers

As mentioned in Making slices, maps and channels:

The built-in function make takes a type T, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions.
It returns a value of type T (not *T).
The memory is initialized as described in the section on initial values.

For instance, for Slice type

make([]T, length, capacity) 

produces the same slice as allocating an array and slicing it, so these two expressions are equivalent:

make([]int, 50, 100) new([100]int)[0:50] 

So here, make creates the slice, and initialize its content depending on the zero value if the type used (here int, so '0')

You can see more about the need of keeping new and make separate in Go: why would I make() or new()?


Dave cheney just wrote a good article: "Go has both make and new functions, what gives ?"

Although make creates generic slice, map, and channel values, they are still just regular values; make does not return pointer values.

If new was removed in favour make, how would you construct a pointer to an initialised value ?

Using new to construct a pointer to a slice, map, or channel zero value works today and is consistent with the behaviour of new.

For the confusion they may cause, make and new are consistent;

  • make only makes slices, maps, and channels,
  • new only returns pointers to initialised memory.
like image 85
VonC Avatar answered Sep 24 '22 17:09

VonC


First difference is type: make(T, ...) always returns type T, whereas new(T, ...) always returns type *T. That tells you that they are very different.

new works for all types, and dynamically allocates space for a variable of that type, initialized to the zero value of that type, and returns a pointer to it.

One way to think of it is that

result = new(T) 

is always equivalent to

var temp T result = &temp 

(in other words you can do the same thing that new does by defining a variable of the given type, not initializing it, and then taking a pointer to it.)

make works as a kind of "constructor" for certain built-in types (slice, map, or channel).

like image 39
newacct Avatar answered Sep 26 '22 17:09

newacct