The difference between new and make in golang

In golang, both make and new allocate memory, but there are still some differences between them. Only by understanding the differences between them can they be used in appropriate occasions.

Simply put, new just allocates memory, not initializes memory; make allocates and initializes memory. The so-called initialization is to assign an initial value to a type, for example, the character is empty, the integer is 0, and the logical value is false.

new

Let's first look at the definition of the new function

// 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

It can be seen that its parameter is a type, the return value is a pointer to the memory address of this type, and the allocated memory will be set to zero, that is, the zero value of the type, that is, the character is empty, the integer is 0, and the logical value is false

Take a look at some examples of new

   type P struct{
        Name string
        Age int
    }
    var a *[2]int
    var s *string
    var b *bool
    var i *int
    var ps *P

    a = new([2]int)
    s = new(string)
    b = new(bool)
    i = new(int)
    ps = new(P) //structure

    fmt.Println(a, " ", *a)
    fmt.Println(s,  " ",*s)
    fmt.Println(b,  " ",*b)
    fmt.Println(i,  " ",*i)
    fmt.Println(ps, " ", *ps)

The output is as follows

&[0 0]   [0 0]
0xc00000e1e0   
0xc00001a07a   false
0xc00001a090   0
&{ 0}   { 0}

The above example is the basic type. Let's look at slice, map and Chan. How to operate with new

    map operation
    var mp *map[string]string
    mp = new(map[string]string)
    //*mp = make(map[string]string) / / if this line is omitted, it will pan "Pan: assignment to entry in nil map"“
    (*mp)["name"] = "lc"
    fmt.Println((*mp)["name"])
    
    slice operation
    var ms *[]string
    ms = new([]string)
    //*ms = make([]string,5) / / if this line is deleted, it will "panic: runtime error: index out of range"
    (*ms)[0] = "lc"
    fmt.Println((*ms)[0])
    

As can be seen above, silce, map, channel and other types are reference types. When the reference type is initialized to nil, nil can not be assigned directly, nor can new be used to allocate memory, but also need to use make to allocate.

make

Look at the function declaration of make

/ The make built-in function allocates and initializes an object of type
// slice, map, or chan (only). Like new, the first argument is a type, not a
// value. Unlike new, make's return type is the same as the type of its
// argument, not a pointer to it. The specification of the result depends on
// the type:
//  Slice: The size specifies the length. The capacity of the slice is
//  equal to its length. A second integer argument may be provided to
//  specify a different capacity; it must be no smaller than the
//  length. For example, make([]int, 0, 10) allocates an underlying array
//  of size 10 and returns a slice of length 0 and capacity 10 that is
//  backed by this underlying array.
//  Map: An empty map is allocated with enough space to hold the
//  specified number of elements. The size may be omitted, in which case
//  a small starting size is allocated.
//  Channel: The channel's buffer is initialized with the specified
//  buffer capacity. If zero, or the size is omitted, the channel is
//  unbuffered.
func make(t Type, size ...IntegerType) Type

It can be seen that it returns the type itself rather than the pointer type, because make can only initialize memory for slice,map,channel, etc., and they return the reference type, so it is unnecessary to return the pointer

Take a look at some examples of make

    mm :=make(map[string]string)
    mm["name"] = "lc"
    fmt.Println(mm["name"])

    mss :=make([]int,2)
    mss[0] = 100
    fmt.Println(mss[0])

    ch :=make(chan int,1)
    ch <-100

    fmt.Println(<-ch)

Summary

make is only used to allocate and initialize data of slice, map and chan types. new can assign any type of data
The new assignment returns a pointer, the Type * Type. make returns a reference, which is Type
The space allocated by new is cleared and initialized after the space allocated by make

Keywords: Go

Added by cljones81 on Tue, 17 Dec 2019 12:02:51 +0200