Go language basic built-in functions new and make

 3 minutes to read

Go language basic built-in functions new and make

The built-in functions make and new in the go language are both used to initialize variables, but they initialize variables in different ways. The return type of make is a reference type, and the return type of new is a pointer type.

built-in function make

The built-in function make is only used to allocate memory space and initialize objects of type slice, map and chan. Same as new, the first argument is the type, not the value. Unlike new, the return type of make is the same as the type of its argument, not a pointer to it.

func make(t Type, size ...IntegerType) Type

The return value depends on the type of argument passed:

Because these three types are reference types, they must be initialized, but not set to zero, which is different from new.

m := make([]int,4)
fmt.Println(m)

m1 := make(map[string]string)
fmt.Println(m1)

m2 := make(chan int)
fmt.Println(m2)

The input result is as follows:

[0 0 0 0]
map[]
0xc00008c060
built-in function new

The built-in function new is only used to allocate memory space, the first parameter is a type, not a value, and the return value is a pointer to a newly allocated zero value of that type.

// 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
  • Also note that it also sets the allocated memory to zero, which is the zero value of the type.

We can declare variables through the var keyword, and then we can use them in the program. When we do not specify the default value of variables, the default value of these variables is their zero value, such as the zero value of int type is 0, the zero value of string type is “”, and the zero value of reference type is nil.

package main

import "fmt"

func main() {
	var i int
	var s string
	fmt.Println("i's value",i)
	fmt.Println("s's value",s)

	var x *int
	fmt.Println(x)
	x=new(int)
	fmt.Println(*x)
	fmt.Println(x)
	*x=10
	fmt.Println(*x)

}

The results are as follows:

i's value 0
s's value
<nil>
0
0xc000128008
10

For a variable of reference type, we not only have to declare it, but also allocate content space for it, otherwise where would our value go?

The declaration of the value type is not needed, because it is already allocated for us by default.

The difference between make and new
  • make is only used to initialize slice, map and chan, new can be used to initialize any type.
  • The return value of make is a “reference type”, and the return value of new is a pointer type.

The built-in function make is required, because slice, map and chan must be initialized (non-zero value) with the built-in function make before they can be used.

The built-in function new is used for type memory allocation, and the memory is set to zero. Usually, the usage scenario is to explicitly return a pointer.