The difference and implementation principle of make and new keywords in Go language

The difference and implementation principle of make and new keywords in Go language

new and make are two built-in functions, which are mainly used to create and allocate types of memory. When we define variables, we may feel a little confused. We don't know which function should be used to declare variables. In fact, their rule is very simple. new only allocates memory, and make can only be used for the initialization of slice, map and channel,
Let's introduce it in detail:

new

In Go language, the new function is described as follows:

// 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

As can be seen from the above code, the new function only accepts one parameter, which is a type and returns a pointer to the memory address of the type. At the same time, the new function will set the allocated memory to zero, that is, the zero value of the type. [example] use the new function to allocate memory space for variables.

var sum *int
sum = new(int) //Allocate space
*sum = 98
fmt.Println(*sum)

Of course, the new function can not only allocate space for the default data type of the system, but also for user-defined types, as shown below:

type Student struct {
name string
age int
}
var s *Student
s = new(Student) //Allocate space
s.name ="dequan"
fmt.Println(s)

Here, if we do not use the new function to allocate space for custom types (comment on line 7), an error will be reported:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x80bd277]
goroutine 1 [running]:

This is the new function, which always returns a pointer to the type, and the pointer points to the memory address of the allocated type.

make

Make is also used for memory allocation, but unlike new, it is only used for memory creation of chan, map and slice, and the types it returns are the three types themselves, not their pointer types. Because these three types are reference types, there is no need to return their pointers. In Go language, the make function is described as follows:

// 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

From the above code, we can see that the t parameter of the make function must be one of chan (channel), map (Dictionary) and slice (slice), and the return value is also the type itself.
Note: the make function is only used for map, slice and channel, and does not return pointers. If you want to get an explicit pointer, you can use the new function for allocation, or explicitly use the address of a variable.

The main differences between new and make in Go language are as follows:

  • make can only be used to allocate and initialize data of type slice, map and chan. new can allocate any type of data;
  • The new assignment returns a pointer, that is, the Type * Type. make returns a reference, that is, Type;
  • The space allocated by new is cleared. make will initialize after allocating space;

summary

Finally, briefly summarize the implementation principle of make and new keywords in Go language. The main function of make keyword is to create slice, map, Channel and other built-in data structures, while the main function of new is to apply for a piece of memory space for the type and return the pointer to this piece of memory.

Keywords: Go Back-end

Added by joplinfan on Thu, 13 Jan 2022 13:07:14 +0200