Source:
Go has both make and new functions, what gives?
Digest:
1. Why can’t we use make for everything?
Although make creates generic slice, map, and channel values, they are still just regular values; make does not return pointer values.
If new was removed in favour make, how would you construct a pointer to an initialised value?
var x1 *int
var x2 = new(int)
x1 and x2 have the same type, *int, x2 points to initialised memory and may be safely dereferenced, the same is not true for x1.
2. Why can’t we use new for everything?
Although the use of new is rare, its behaviour is well specified.
new(T) always returns a *T pointing to an initialised T. As Go doesn't have constructors, the value will be initialised to T's zero value.
Using new to construct a pointer to a slice, map, or channel zero value works today and is consistent with the behaviour of new.
s := new([]string)
fmt.Println(len(*s)) // 0
fmt.Println(*s == nil) // true
m := new(map[string]string)
fmt.Println(m == nil) // false
fmt.Println(*m == nil) // true
c := new(chan int)
fmt.Println(c == nil) // false
fmt.Println(*c == nil) // true
3. Sure, but these are just rules, we can change them, right?
For the confusion they may cause, make and new are consistent; make only makes slices, maps, and channels, new only returns pointers to initialised memory.
4. In summary:
make and new do different things.
My advice is to use new sparingly, there are almost always easier or cleaner ways to
write your program without it.