Fundamentals 8 min read

Why Go Requires Both new and make for Memory Allocation

Go provides two distinct constructors—new, a generic memory allocator returning a zero‑initialized pointer, and make, a specialized initializer that allocates and prepares slices, maps, and channels—explaining why both exist, how they differ, common pitfalls, and the language’s design philosophy of explicit, ready‑to‑use values.

Golang Shines
Golang Shines
Golang Shines
Why Go Requires Both new and make for Memory Allocation

1. The new Operator

new is a generic memory allocator. It allocates enough memory for a value of type T, zeroes the memory, and returns a *T pointer. Example:

p := new(int) // p points to an int initialized to 0

The pointer can be dereferenced and assigned, e.g., *p = 10.

2. The make Function

make is a built‑in for three reference types: slice, map, and chan. It allocates the underlying data structure and initializes internal fields (length, capacity, pointers) so the result is immediately usable. It returns the value itself, not a pointer. Example:

m := make(map[string]int) // ready to assign m["key"] = 1

3. Core Difference

new returns *T, while make returns T. Using new(map[string]int) yields a *map[string]int that is a pointer to a nil map; writing to it panics ("assignment to entry in nil map"). Using make with a non‑reference type such as int or a struct is a compile‑time error because those types are not supported.

4. Design Philosophy

Go follows the principle “explicit over implicit”. new only guarantees memory allocation and zeroing; it does not guarantee that the value is usable. make guarantees that the reference type is fully initialized and ready for use. This separation avoids hidden initialization costs for ordinary types while ensuring correct setup for reference types.

5. Practical Guidance

In everyday code, use make for slices, maps, and channels because they are the three main Go data structures. new is rarely needed; Go prefers literals (e.g., &T{}) or plain var declarations for other types.

When you see the runtime panic "assignment to entry in nil map", replace the new call with make for the map.

6. Quick Comparison

Applicable Types : new works for all types (int, struct, array …); make only for slice, map, chan.

Return Value : new returns *T (pointer); make returns T (value).

Main Action : new performs allocation + zeroing; make performs allocation + initialization of internal structures.

Result State : new yields a zero value that may be unusable for reference types; make yields a ready‑to‑use value.

Common Errors : using new with map/slice leads to panic; using make with int/struct causes compile error.

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 allocationchannelreference typesmakenew
Golang Shines
Written by

Golang Shines

We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.

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.