Fundamentals 7 min read

Understanding Go’s new vs. make: When and Why to Use Each

This article explains the shared behavior of Go's new and make functions, highlights their key differences—including supported types, return values, and initialization details—and provides concrete code examples to show how each should be used in practice.

Subtle Storm
Subtle Storm
Subtle Storm
Understanding Go’s new vs. make: When and Why to Use Each

Common Points of new and make

Both allocate memory and return a pointer to the allocated block.

Both involve memory allocation, but apply to different types.

Both return a pointer to a type.

Both initialize the allocated memory to the zero value of the type (e.g., 0 for integers, false for booleans, nil for pointers).

Key Differences (Two Points to Remember)

new is used for any type (basic types, structs, arrays, etc.) and returns a pointer to that type.

make is used exclusively for slices, maps, and channels; it not only allocates memory but also initializes the internal state of these data structures so they can be used directly.

Comparison Summary

Purpose : new allocates memory for any type and returns *T; make allocates and initializes slices, maps, or channels and returns the value itself ( T).

Return : new returns a pointer ( *T); make returns the concrete type ( T).

Supported Types : new works with any type; make works only with slice, map, and channel.

Initialization : new zero‑initializes the memory; make also sets up the internal structures of slices, maps, or channels.

Typical Use Cases : use new when you need a pointer to a basic type or struct; use make when you need a ready‑to‑use slice, map, or channel.

Examples : new(int), new(Person) vs. make([]int, 5), make(map[string]int), make(chan int).

Special Note : When using new, the result is a pointer, so you must dereference it to access the actual value. make returns the value itself, which can be used directly.

1. Detailed Analysis of new

new is a built‑in function that allocates memory for a specified type and returns a pointer to that type ( *T).

The allocated memory is zero‑initialized.

Applicable to any type: basic types, structs, arrays, etc.

Code demonstration:

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    // Use new to allocate memory and zero‑initialize
    p := new(Person) // returns *Person
    fmt.Println(p)      // prints pointer (zero value)
    fmt.Println(*p)     // prints zero‑initialized struct {"" 0}

    // Modify fields via the pointer
    p.Name = "Alice"
    p.Age = 30
    fmt.Println(*p)     // prints {Alice 30}
}

Output:

&{ } // pointer to Person (zero value)

{ 0} // zero‑initialized Person

{Alice 30} // Person after modification

2. Detailed Analysis of make

make is a built‑in function for initializing slices, maps, and channels.

It allocates memory and also initializes the internal data structures so the collection can be used immediately.

Returns the concrete type itself, not a pointer.

Only applicable to slice, map, and channel types.

Code demonstration:

package main

import "fmt"

func main() {
    // Create and initialize a slice
    s := make([]int, 5) // length 5 slice
    fmt.Println(s)          // [0 0 0 0 0]

    // Create and initialize a map
    m := make(map[string]int) // empty map
    m["Alice"] = 30
    fmt.Println(m)            // map[Alice:30]

    // Create and initialize a channel
    ch := make(chan int) // unbuffered channel
    go func() { ch <- 100 }()
    fmt.Println(<-ch)      // 100
}

Output:

[0 0 0 0 0] // initial slice values

map[Alice:30] // map content

100 // value received from channel

Hope this helps you make more informed memory‑management decisions when writing Go code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Gomapslicememory allocationchannelpointermakenew
Subtle Storm
Written by

Subtle Storm

The micro era's marvels are boundlessly subtle.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.