SoFunction
Updated on 2025-03-05

Detailed explanation of the similarities and differences between make and new in golang

Introduction

In Go,makeIt is a built-in function for creating reference types such as slices, maps, and channels.makeThe main function is to allocate memory and initialize these reference types.

usage:

1. Create slice:

slice := make([]int, 5) // Create a slice containing 5 integers

makeAccept two parameters, the first is type and the second is length. For slices, an optional capacity parameter can also be provided:

slice := make([]int, 5, 10) // Create a slice with a length of 5 and a capacity of 10

2. Create a map:

myMap := make(map[string]int) // Create a string to integer mapping

3. Create a channel:

ch := make(chan int) // Create an integer type channel

Notes:

  1. makeIt can only be used for reference type data structures, and cannot be used for the creation of value types (such as structures).
  2. makeReturns the initialized reference type instance, not a pointer.
  3. For slices and mappings,makeIn addition to allocating memory, internal data structures are also initialized to ensure that they can be used directly.
  4. For the channel,makeAn unbuffered channel will be created and returned.

Overall,makeMainly used to create reference types and initialize them to ensure that they can be used correctly.

New Introduction

In Go,newis a built-in function for allocating memory for a value type and returning a pointer to a newly allocated zero-value instance.newMainly used to create instances of value types, such as structures. The following isnewBasic usage and precautions:

usage:

1. Create a value type instance:

ptr := new(int) // Create an integer type pointer to the newly allocated zero-valued integer

2. Create a structure instance:

type MyStruct struct {
    Field1 int
    Field2 string
}

instance := new(MyStruct) // Create a pointer of type MyStruct to the newly allocated zero-value structure

Notes:

  1. newReturns a pointer to a zero-value instance of newly allocated memory.
  2. For value types,newThe allocated memory will be initialized to a zero value.
  3. newAccepts a parameter, that is, the type to allocate memory, and returns a pointer to the zero value of that type.
  4. newNot applicable to reference types (such as slices, maps, and channels), they can only be used for the creation of value types.
  5. newThe allocated memory will not be cleaned up and the programmer needs to be responsible for freeing it.

Overall,newMainly used to allocate memory for value types and return a pointer to a newly allocated zero-value instance. When using it in actual use, choose to use it as needednewOr declare and initialize directly.

The similarities and similarities between make and new

makeandnewThey are all built-in functions for memory allocation in Go, but their uses and behaviors have some key differences.

Similarities:

  • Memory allocation:Both are used to allocate memory on the heap.

Differences:

1. Applicable types:

  • makemake: only forslicemapas well aschannelInitialization.
slice := make([]int, 5)
  • newUsed to create instances of value types, such as structures. Returns a pointer to the newly allocated zero-value instance.
ptr := new(int)

2. Return type:

  • makeReturns the initialized reference type instance, not a pointer.

  • newReturns a pointer to the newly allocated zero-value instance.

3. Parameters:

  • makeAccept parameters such as type and length to initialize the internal data structure of the reference type.
slice := make([]int, 5)
  • newOnly accept type parameters, returning a pointer to the zero value of that type.
ptr := new(int)

4. Initialize:

  • makeIn addition to allocating memory, internal data structures of reference types are also initialized.

  • newThe allocated memory will be initialized to a zero value.

Summarize:

  • usemakeMainly used to initialize reference types to ensure that they can be used directly.
  • usenewMainly used to allocate memory for value types, returning a pointer to a newly allocated zero-value instance.

Choose to usemakestillnewDepends on the type of data structure you are creating and the initialization requirements.

The above is a detailed explanation of the similarities, similarities and usage of make and new in golang. For more information on the similarities, similarities and usage of golang make and new, please pay attention to my other related articles!