Why would I make() or new()?

前端 未结 7 1946
面向向阳花
面向向阳花 2020-11-29 14:27

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

7条回答
  •  情歌与酒
    2020-11-29 15:30

    You need make() to create channels and maps (and slices, but those can be created from arrays too). There's no alternative way to make those, so you can't remove make() from your lexicon.

    As for new(), I don't know of any reason offhand why you need it when you can use struct syntax. It does have a unique semantic meaning though, which is "create and return a struct with all fields initialized to their zero value", which can be useful.

提交回复
热议问题