This is a creation in Article, where the information may have evolved or changed.
The built-in functions in the go language new and make are the two primitives used for memory allocation (allocation primitives). For beginners, the difference between the two is also quite easy to confuse. Simply put, new allocates only memory, make is used for Slice,map, and the channel is initialized.
1. New function
The new build-in function allocates memory (only allocates space). The first argument is a type, not a value, and the value returned are a pointer to a newly allocated zero value of that Typ E.
Translate as follows:
Built-in function new allocates space. Passed to the new function is a type, not a value. The return value is a pointer to the newly assigned 0 value.
This is a built-in function for allocating memory, but unlike C + +, it does not initialize memory, it simply resets it to zero. That is, new (t) assigns a zero-zeroed store to the type T, and returns its address, a value of type *t. In the Go terminology, it returns a pointer to the newly assigned type T, which points to a value of 0 (zero value). Note that the pointer is not zero.
The object in the go language does not have a constructor in C + +, and if you use C to describe it, the new in Go is probably equivalent to:
T *t = (T*)malloc(sizeof(T))memset(t, 0, sizeof(T))
In fact, the above description may not be very accurate, perhaps with *t=zerovalue more accurate. Because of the different data types, the meaning of the value of 0 is not exactly the same. For example, for a bool type, a value of 0 with a value of False;int of 0 is an empty string of 0 0;string:
b := new(bool) fmt.Println(*b) i := new(int) fmt.Println(*i) s := new(string) fmt.Println(*s)
Output:
False
0
Note that there is an empty string at the end.
Initialization
Many times, a value of 0 is not a good idea and we need to do some initialization. Consider the following structural body:
type Rect struct { x, y float64 width, height float64}
A 0 value rect is not of much use, and we initialize it in the following ways:
RECT3: = &rect{0, 0, 100, 200}
Rect4: = &rect{width:100, height:200}
The go language does not have constructors in C + +, and the creation of objects is generally done by a global creation function:
func NewRect(x, y, width, height float64) *Rect { return &Rect{x, y, width, height}}
Note that in this case, unlike C + +, it is absolutely no problem to return the address of a local variable in the Go language; the store of the variable association persists after the function returns.
More directly, in the go language, if a local variable is still used after the function is returned, the variable allocates memory from the heap, not the stack.
2. Make
The Make built-in function allocates and initializes an object (allocate space + initialize) of type slice, map or Chan(only). Like new, the first arguement was a type, not a value. Unlike new, make's return type is the same as the type of its argument, and not a pointer to it. The specification of the result depends on the type.
Translated as:
The built-in function make assigns and initializes a slice, or map or Chan object. And can only be these three kinds of objects. As with new, the first argument is a type, not a value. But the return value of make is this type (even if it is a reference type), not a pointer. The specific return value that depends on the specific incoming type.
The built-in function make (t, args) is not the same as the purpose of new (T). It is used only to create SLICE,MAP and channel, and returns an initialized (instead of 0) with a value of type T (not *t). This is different because these three types have references to data structures that must be initialized before they are used. For example, slice is a ternary descriptor that contains a pointer to the data (in the array), the length, and the capacity, before the entries are initialized, and slice are nil. Initialize these internal data structures for Slice,map and Channel,make, and prepare the available values.
For example
make([]int, 10, 100)
Assigns an array of 100 int, and then creates a slice structure with a length of 10 and a capacity of 100, which references an array containing the first 10 elements. Correspondingly, new ([]int) returns a pointer to the newly assigned, zeroed slice struct, which points to a slice with a value of nil.
var p *[]int = new([]int) // allocates slice structure; *p == nil; rarely usefulvar v []int = make([]int, 100) // the slice v now refers to a new array of 100 ints// Unnecessarily complex:这种做法实在是很蛋疼var p *[]int = new([]int)*p = make([]int, 100, 100)// Idiomatic:习惯的做法v := make([]int, 100)
Make is used only for Map,slice and channel, and does not return a pointer. To get an explicit pointer, use new to assign, or explicitly use the address of a variable. Unlike new, make returns a reference to a type instead of a pointer.