Why would I make() or new()?

The introduction documents dedicate many paragraphs to explaining the difference between new() and make() , but in practice, you can create objects within local scope and return them.

Why would you use the pair of allocators?

1 Like

Things you can do with make that you can’t do any other way:

  • Create a channel
  • Create a map with space preallocated
  • Create a slice with space preallocated or with len != cap

It’s a little harder to justify new . The main thing it makes easier is creating pointers to non-composite types. The two functions below are equivalent. One’s just a little more concise:

func newInt1() *int { return new(int) }

func newInt2() *int {
    var i int
    return &i

Refer: stackoverflow.com