new, make and zero values in Go
new(T)
returns a pointer to a newly allocated zero value of type T
.
make
returns an initialised slice, map or channel. For example, a slice which points to an underlying array (an uninitialised slice does not).
In Go, the zero value is a default value assigned to an allocated but uninitialised variable. Each type has its own zero value. For slices and maps, the zero value is nil
.
Once assigned to a variable, nil
is typed and behaves accordingly. You can append
to a nil
slice and look up keys in a nil
map, but not vice versa. You cannot compare nil
s of different types.