SoFunction
Updated on 2025-03-05

The difference between GO language make and new keywords

  • make usage scenario slice map channel
  • new usage scenarios Most of them are used in structures

1. new and make

Execution error

Executing the following code will cause panic, why?

  • In Go language, for reference type variables, we not only need to declare them when using them, but also allocate memory space for them, otherwise our values ​​will not be able to be stored.
  • The memory space is not required for declarations of value types because they have already allocated memory space by default when declaring.
  • To allocate memory, today's new and make are drawn out. In Go language, new and make are two built-in functions, mainly used to allocate memory.
func main() {
var userinfo map[string]string
    userinfo["username"] = "Zhang San"
    (userinfo)
}
/*
panic: assignment to entry in nil map
*/

2. Comparison between make and new

new and make are two built-in functions that are mainly used to create and allocate types of memory.

The difference between make and new

  • The purpose of the make keyword is to create built-in data structures such as slice, map, and channel
  • The function of new is to apply for a memory space for the type and return a pointer to this memory (apply for space and return pointer)
func main() {
a := make([]int, 1, 10) // The slice length is 1, and the reserved space length is 10a = append(a,1)
("%v--%T \n",a,a) // [0 0 0]--[]int value----slice itselfvar b = new([]int)
//b = (b,2) // The returned memory pointer, so you cannot directly append*b = append(*b, 3) // You must use the * pointer to get the value before append is added("%v--%T",b,b) // &[]--*[]string Memory pointer---Memory pointer}

3. new function

The default data type of the system, allocating space

// Instantiate intage := new(int)
*age = 1
// Instantiate slicesli := new([]int)
*li = append(*li, 1)
// 3. Instantiate mapuserinfo := new(map[string]string)
*userinfo = map[string]string{}
(*userinfo)["username"] = "Zhang San"
(userinfo) // &map[username: Zhang San]}

Custom types use new functions to allocate space

func main() {
var s *Student
s = new(Student) //Allocate space ="zhangsan"
(s) // &{zhangsan 0}
}
type Student struct {
name string
age int
}

4. Make function

  • make is also used for memory allocation, but unlike new, it is only used for memory creation of chan, map and slice and the type it returns is these three types themselves, not their pointer type.
  • Because these three types are reference types, there is no need to return their pointers
package main
import "fmt"
func main() {
a := make([]int,1,10) // The slice length is 1, and the reserved space length is 10b := make(map[string]string)
c := make(chan int, 1)
(a,b,c) // [0 0 0] map[] 0xc0000180e0
}

When we allocate memory for slices, we should try to estimate the maximum possible length of slices, and reserve memory space for slices by passing a third parameter to make. This can avoid the overhead caused by secondary allocation of memory and greatly improve the performance of the program.

This is the end of this article about the difference between GO language make and new keywords. For more related Go language new and make content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!