This article introduces a very common interview question. How common is it? This is probably the beginning of many interviews. That is the difference between the two built-in functions, new and make.
In fact, this problem itself is not complicated. Simply put, new only allocates memory, while make can only be used for the initialization of slice, map and chan. Let's introduce it in detail below.
new
new is a built-in function that allocates a piece of memory and returns a pointer to that memory.
The function signature is as follows:
Source code
// The new built-in function allocates memory. The first argument is a type, // not a value, and the value returned is a pointer to a newly // allocated zero value of that type. func new(Type) *Type
From the above code, we can see that the new function only accepts one parameter, which is a type, and returns a pointer to the memory address of that type.
At the same time, the new function will place the allocated memory to zero, that is, the zero value of the type.
use
Use the new function to allocate memory space to variables:
p1 := new(int) ("p1 --> %#v \n ", p1) //(*int)(0xc42000e250) ("p1 point to --> %#v \n ", *p1) //0 var p2 *int i := 0 p2 = &i ("p2 --> %#v \n ", p2) //(*int)(0xc42000e278) ("p2 point to --> %#v \n ", *p2) //0
The above code is equivalent,new(int)
Initialize the allocated space to the zero value of int, that is, 0, and return the pointer of int, which is the same as directly declaring the pointer and initializing it.
Of course, the new function can not only allocate space for the system's default data type, but also use the new function to allocate space for custom types, as shown below:
type Student struct { name string age int } var s *Student s = new(Student) //Allocate space = "zhangsan" (s)
This is the new function, which always returns a pointer of type, pointer pointing to the memory address of the allocated type. It should be noted that the new function will only allocate memory space, but will not initialize that memory space.
make
make is also used for memory allocation, but unlike new, it is only used for memory creation of slice, map, and chan, and the type it returns is these three types themselves, not their pointer type. Because these three types are reference types themselves, there is no need to return their pointers.
The function signature is as follows:
Source code
// The make built-in function allocates and initializes an object of type // slice, map, or chan (only). Like new, the first argument is a type, not a // value. Unlike new, make's return type is the same as the type of its // argument, not a pointer to it. The specification of the result depends on // the type: // Slice: The size specifies the length. The capacity of the slice is // equal to its length. A second integer argument may be provided to // specify a different capacity; it must be no smaller than the // length, so make([]int, 0, 10) allocates a slice of length 0 and // capacity 10. // Map: An empty map is allocated with enough space to hold the // specified number of elements. The size may be omitted, in which case // a small starting size is allocated. // Channel: The channel's buffer is initialized with the specified // buffer capacity. If zero, or the size is omitted, the channel is // unbuffered. func make(t Type, size ...IntegerType) Type
Through the above code, you can see the make function'st
The parameters must be one of slice, map, and chan, and the return value is also the type itself.
use
Here is an example using slice:
var s1 []int if s1 == nil { ("s1 is nil --> %#v \n ", s1) // []int(nil) } s2 := make([]int, 3) if s2 == nil { ("s2 is nil --> %#v \n ", s2) } else { ("s2 is not nill --> %#v \n ", s2)// []int{0, 0, 0} }
The zero value of slice isnil
, but after initialization with make, the slice content is filled with zero value of type int, such as:[]int{0, 0, 0}
。
Map and chan are also similar, so I won’t say much.
Summarize
Through the above analysis, we can summarize the main differences between new and make as follows:
- make can only be used to allocate and initialize data of types slice, map, and chan. new can allocate any type of data;
- The new allocation returns a pointer, that is, type
*Type
. make returns the type itself, i.e.Type
; - new allocated space is cleared. After making allocates space, it will be initialized;
This is the end of this article about explaining the differences between new and make keywords in Go. For more related Go language new make content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!