If you have any questions or suggestions, please contact us in time. My official account is “Brain fried Fish”, GitHub address: github.com/eddycjy.

Hello, I’m fried fish.

In Go, there are two similar built-in functions, new and make methods, which are mainly used to allocate the corresponding type of memory space.

It looks like new and make both allocate memory, so what’s the difference? This detail has become one of the interview questions for many Go language engineers, which is worth taking a look at.

We will answer that question in today’s article.

The basic features

make

In Go, the built-in function make supports memory creation of only three data types, slice, map, and channel. The return value is the created type itself, rather than a new pointer reference.

The function signature is as follows:

func make(t Type, size ... IntegerType) Type
Copy the code

Specific usage examples:

func main(a) {
	v1 := make([]int.1.5)
	v2 := make(map[int]bool.5)
	v3 := make(chan int.1)
    
	fmt.Println(v1, v2, v3)
}
Copy the code

In the code, we initialize each of the three types by calling the make function. You’ll notice that some input parameters have multiple lengths specified and some don’t.

The main difference is the length (len) and capacity (cap) specified, some types do not have the capacity of the statement, so naturally cannot be specified.

Output result:

[0] map[] 0xc000044070
Copy the code

One detail to note is that when the make function is called to initialize the type of the slice, it has a zero value and needs to be specified.

I’ve seen a lot of guys stomp holes in it.

new

In the Go language, the built-in function new creates and initializes types in memory. The return value is a pointer reference to the created type, which differs in substantive detail from the make function.

The function signature is as follows:

func new(Type) *Type
Copy the code

Specific usage examples:

type T struct {
	Name string
}

func main(a) {
	v := new(T)
	v.Name = "Fish"
}
Copy the code

Does the above example have a similar effect? In fact, the same way as the following:

Func main() {v := T{} v.name = "fish"}Copy the code

The output results are as follows:

& {Name: fish}Copy the code

In fact, the new function is relatively rare in everyday engineering code because it can be replaced.

It is generally used to initialize directly with the shortcut T{}, because normal structs have literal properties of structs:

Func NewT() *T {return &T{Name: "fried fish "}}Copy the code

This method of initialization is more convenient.

What’s the difference

The new function can also initialize three types of make:

	v1 := new(chan bool)
	v2 := new(map[string]struct{})
Copy the code

So what’s the difference, what’s the advantage of make?

The make function initializes the slice, chan, and map internal data structures, while the new function does not.

For example, in map types, reasonable length (len) and capacity (CAP) can improve efficiency and reduce overhead.

A further distinction:

  • makeFunction:
    • The ability to create the required memory space for a type, returning the reference type itself.
    • Has the limitation of use scope, only supportedchannel,map,sliceThree types.
    • With unique advantages,makeThe function assigns values to three types of internal data structures (length, capacity, and so on).
  • newFunction:
    • The ability to create and allocate the required memory space for a type, returning a pointer reference (a pointer to memory).
    • Can be replaced and can be initialized quickly via literals.

conclusion

In this article, we introduce the use of make and new functions in Go language, and analyze their differences.

If the new and make functions initialize memory, do they allocate it to the heap or to the stack?

This comes down to “escape analysis” in Go, where if the initialized variable does not need to live outside of the current scope, then in theory it does not need to be initialized on the heap.

My official account

Share Go language, micro service architecture and strange system design, welcome to pay attention to my public number and I exchange and communication.

The best relationship is mutual achievement. Your praise is the biggest motivation for the creation of fried fish. Thank you for your support.