What is the difference between make and new in Go?
Learn the difference between make and new in Go: new returns a pointer to a zero value, while make initializes slices, maps and channels for immediate use.
Expected Interview Answer
In Go, new(T) allocates zeroed memory for a type and returns a pointer *T, while make() only builds and initializes slices, maps, and channels and returns an initialized value of that type (not a pointer).
new works for any type and gives you a pointer to a zero value, so new(int) yields a *int pointing at 0. make is special-cased for the three built-in reference types because they need internal bookkeeping — a slice needs a backing array, length and capacity; a map needs its hash table; a channel needs its buffer — which a plain zeroed value lacks. That is why make returns a ready-to-use value rather than a pointer.
- make gives usable slices, maps and channels immediately
- new provides a pointer to a zeroed value of any type
- Prevents assigning to nil maps by initializing them
- Clarifies intent: allocation vs. initialization
- Avoids runtime panics from uninitialized reference types
AI Mentor Explanation
Think of new as being handed an empty, unmarked scorebook — the pages exist but nothing is set up, like a pointer to a blank value. make is the groundsman preparing the whole pitch before a match: rolling it, marking the creases, setting the stumps, so play can start instantly. A slice, map or channel from make arrives match-ready, whereas new only gives you the blank book.
Step-by-Step Explanation
Step 1
Pick the type
Decide whether you need a pointer to a zeroed value (any type) or a ready reference type.
Step 2
Use new for pointers
Call new(T) when you want a *T pointing at the zero value, e.g. new(int) or new(MyStruct).
Step 3
Use make for reference types
Call make for slices, maps and channels: make([]int, 0, 10), make(map[string]int), make(chan int).
Step 4
Supply sizing hints
For make, pass length/capacity for slices or buffer size for channels to control allocation.
Step 5
Use the result
make values are immediately usable; new values are pointers you dereference and populate.
What Interviewer Expects
- Knowing new returns a pointer and make returns a value
- Awareness that make only works on slice, map and channel
- Understanding zero values and reference-type initialization
- Explaining why a nil map cannot be written to
- A concrete code example distinguishing the two
Common Mistakes
- Saying make returns a pointer
- Trying to use make on a struct or int
- Assuming new gives a usable map or channel
- Writing to a nil map instead of make-initializing it
- Confusing capacity with length for slices
Best Answer (HR Friendly)
“In Go, new sets aside empty memory for any type and hands back a pointer to it, while make is used only for slices, maps and channels and gives back a ready-to-use value. Put simply, new reserves space, and make fully prepares the three built-in collection types so you can start using them right away.”
Code Example
// new: pointer to a zeroed value
p := new(int) // p is *int, *p == 0
*p = 42
s := new([]int) // s is *[]int pointing at a nil slice
// make: initialized reference types
sl := make([]int, 0, 10) // slice, len 0, cap 10
m := make(map[string]int) // usable map
ch := make(chan int, 5) // buffered channel
m["go"] = 1 // works; a nil map would panic hereFollow-up Questions
- Why can you not use make with a struct type?
- What is the zero value of a slice, map and channel?
- What happens if you write to a nil map?
- How do length and capacity differ when making a slice?
- When would you prefer new(T) over &T{}?
MCQ Practice
1. What does new(int) return?
new(T) allocates zeroed memory and returns a pointer to it, so new(int) yields a *int that points at 0.
2. Which types can make be used with?
make is special-cased for the three built-in reference types: slices, maps and channels.
3. What does make return?
Unlike new, make returns an initialized, ready-to-use value (not a pointer).
Flash Cards
What does new(T) return? — A pointer *T to a zeroed value of type T.
What does make return? — An initialized value (not a pointer) of a slice, map or channel.
Which types work with make? — Only slices, maps and channels.
Why can't you write to a nil map? — It has no allocated hash table; use make(map...) first or it panics.
Continue Learning
Related Interview Questions
What is a nil pointer, nil slice, and nil map in Go and how do they behave?
medium
How does the underlying array, length, and capacity of a Go slice work?
medium
What is a map in Go and is it safe for concurrent use?
medium
What are channels in Go and how do they enable communication between goroutines?
medium