SoFunction
Updated on 2025-03-05

A detailed explanation of the difference between new and make in Golang

1. Introduction

In Go, new and make are two built-in functions used to create objects, and their usage and functions are different. A correct understanding of the difference between new and make is very important for writing efficient Go code. Below we will introduce the specific details of new and make step by step.

2. new function

2.1 The role of new function

The new function is used to create a pointer of the specified type and initialize it to a zero value. It returns a pointer to the newly allocated memory address.

2.2 Syntax of new function

The syntax of the new function is very simple, just follow the type after the keyword new.

 func new(Type) *Type

Here is a sample code for creating a structure object using the new function:

 package main
 ​
 import "fmt"
 ​
 type Person struct {
     Name string
     Age  int
 }
 ​
 func main() {
     p := new(Person)
     (p)
 }

In the above code, we use the new function to create a pointer p to type Person and output its value. The result of the run is &{ 0}, indicating that p is a pointer to a zero value of type Person.

3. make function

3.1 The role of make function

The make function is used to create objects of built-in types such as slices, maps, and channels, which returns an initialized (non-zero value) object.

3.2 Syntax of make function

The syntax of the make function is different from that of the new function, which requires specifying the type and some extra parameters.

 func make(Type, size IntegerType) Type

Here are some example codes that use the make function to create slices, maps, and channels:

 package main
 ​
 import "fmt"
 ​
 func main() {
     // Create an integer slice with a length of 5 and a capacity of 10     s := make([]int, 5, 10)
     (s)
     
     // Create a map with a key string and a value of int     m := make(map[string]int)
     m["a"] = 1
     m["b"] = 2
     (m)
     
     // Create a string channel     c := make(chan string)
     (c)
 }

In the above code, we use the make function to create an integer slice s with length 5 and capacity 10, a map m with a key string and a value of int, and create a string channel c. The run results are [0 0 0 0 0], map[a:1 b:2] and 0xc0000460c0, respectively, indicating that these objects are initialized to non-zero values.

4. Differences and comparisons

4.1 Types of assignment

  • The new function is used for any type of allocation and returns a pointer to that type.
  • The make function is only used to allocate slices, maps, and channels, and returns the initialized slices, maps, or channel objects.

4.2 Return value type

  • The new function returns a pointer to the allocation type.
  • make Returns the initialized non-zero value of the allocation type.

4.3 Use scenarios

  • The new function is mainly used to create instances of value types. Value types include basic types (such as integer, floating point, boolean, etc.) and structures. The new function returns a pointer to the newly allocated memory, which can easily operate and modify the instance.

 package main
 ​
 import "fmt"
 ​
 type Point struct {
     X, Y int
 }
 ​
 func main() {
     p := new(Point)
      = 10
      = 20
     (p) // Output: &{10 20} }
  • The make function is mainly used to create objects of reference type. Reference types include slices, maps, and channels. Since the reference type needs to be initialized before use, the make function returns an initialized non-zero value object, not a pointer.
 package main
 ​
 import "fmt"
 ​
 func main() {
     // Create an integer slice with a length of 5 and a capacity of 10     s := make([]int, 5, 10)
     (s) // Output: [0 0 0 0 0]     
     // Create a map with a key string and a value of int     m := make(map[string]int)
     m["a"] = 1
     m["b"] = 2
     (m) // Output: map[a:1 b:2]     
     // Create a string channel     c := make(chan string)
     (c) // Output: 0xc0000460c0 }

4.4 Sample code comparison

By comparing the following sample code, we can understand the difference between new and make more clearly:

 package main
 import "fmt"
 ​
 type Person struct {
     Name string
     Age int
 }
 ​
 func main() {
     // Create a pointer of type Person using new     p1 := new(Person)
      = "Alice"
      = 25
     (p1) // Output: &{Alice 25}     // Create slices with make     s1 := make([]int, 5, 10)
     s1[0] = 1
     s1[1] = 2
     (s1) // Output: [1 2 0 0 0] }

As can be seen from the sample code, using new creates a pointer type, while using make creates an initialized non-zero value object.

5. Summary

In this article, we introduce in detail the difference between new and make and usage scenarios in Golang. By analyzing the syntax and sample code of both, we draw the following conclusions:

  • new is used for any type of allocation and returns a pointer to that type, mainly used to create instances of value types.
  • make is used to allocate slices, maps, and channels, and returns an initialized non-zero value object, mainly used to create reference-type objects.

A proper understanding of the difference between new and make is essential for writing efficient and standardized Go code. When choosing to use new or make, you must make judgments based on the specific requirements and object type. I hope this article will be helpful to you and can understand and apply new and make functions more deeply. Thanks for reading!

The above is a detailed article about using new and make to build Golang applications. For more information about using new and make to build Golang applications, please follow my other related articles!