The Difference between new and make in Go

Demystify Go programming by understanding the distinctions between 'new' and 'make'. Learn when and how to use each effectively for optimal code efficiency.
E
Edtoks2:52 min read

In Go, new and make are both used to allocate memory, but they are used for different types and have different purposes.

new:

The new keyword is used to allocate memory for a new zero-initialized value of a specified type. It returns a pointer to the allocated memory. new only initializes the memory and does not initialize the individual elements of a composite type like slices or maps.

Example 1: new for Basic Types

package main

import "fmt"

func main() {
    // Allocating memory for an integer
    ptr := new(int)

    // Assigning a value to the allocated memory
    *ptr = 42

    fmt.Println(*ptr)
}

Example 2: new for Structs

package main

import "fmt"

type Point struct {
    X, Y int
}

func main() {
    // Allocating memory for a struct
    ptr := new(Point)

    // Assigning values to the struct fields
    ptr.X = 10
    ptr.Y = 20

    fmt.Println(*ptr) 
}

make:

The make keyword is specifically used for creating slices, maps, and channels. It initializes and returns an initialized value of a specified type. Unlike new, make initializes the individual elements of the composite types.

Example 3: make for Slices

package main

import "fmt"

func main() {
    // Creating a slice of integers with make
    mySlice := make([]int, 3) // Creates a slice with length 3 and capacity 3

    // Assigning values to the slice elements
    mySlice[0] = 1
    mySlice[1] = 2
    mySlice[2] = 3

    fmt.Println(mySlice) 
}

Example 4: make for Maps

package main

import "fmt"

func main() {
    // Creating a map with make
    myMap := make(map[string]int)

    // Assigning values to the map
    myMap["a"] = 1
    myMap["b"] = 2

    fmt.Println(myMap) // Output: map[a:1 b:2]
}

Internals:

new Internals:

new allocates memory for a zero-initialized value of the specified type using the malloc function from the underlying memory allocator. The returned value is a pointer to the newly allocated memory.

make Internals:

make is used for slices, maps, and channels, which are composite types. Internally, make initializes the underlying data structure and returns an initialized value. For slices, it creates an underlying array and a slice structure. For maps, it initializes the map structure and creates an initial hash table.

Summary

  • new allocates memory for a new value of any type and returns a pointer to that memory.

  • make allocates and initializes an object of type slice, map, or chan only.

In general, use make to create slices, maps, and channels, and use new to create other types.

Conclusion

Understanding the difference between new and make is crucial for correctly allocating and initializing memory in Go. Use new for basic types and make for slices, maps, and channels.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.