-
Book Overview & Buying
-
Table Of Contents
Practical Systems Programming in Go
By :
In Go, both make and new allocate memory, but they serve different purposes and reflect different design philosophies of the language. At first glance, they seem interchangeable – after all, both produce usable values – but their roles are distinct. The new built-in is minimalistic: it allocates zeroed memory for any type and returns a pointer to it, leaving initialization entirely to you. In contrast, make is specialized and opinionated: it works only with slices, maps, and channels, allocating and initializing the underlying data structures so they are immediately ready to use. Psychologically, you can think of new as giving you a blank sheet of paper, while make hands you a fully bound notebook, ready for writing.
For example, p := new(int) gives you a pointer to an integer initialized to 0. Think of new as handing you an empty box, and you decide what to put inside it, whereas s := make([]int, 5) creates a slice of five integers initialized to zero, and...