Konubinix' opinionated web of thoughts

New() vs Make() in Golang

Fleeting

new() vs make() in golang go

three different ways to create a pointer p that points to a zeroed bytes.Buffer value, each of which are equivalent:

/ Allocate enough memory to store a bytes.Buffer value / and return a pointer to the value’s address. var buf bytes.Buffer p := &buf

/ Use a composite literal to perform allocation and / return a pointer to the value’s address. p := &bytes.Buffer{}

/ Use the new function to perform allocation, which will / return a pointer to the value’s address. p := new(bytes.Buffer)

https://www.godesignpatterns.com/2014/04/new-vs-make.html

make() function, on the other hand, is a special built-in function that is used to initialize slices, maps, and channels. Note that make() can only be used to initialize slices, maps, and channels, and that, unlike the new() function, make() does not return a pointer

https://www.godesignpatterns.com/2014/04/new-vs-make.html

Slices, maps, and channels can also be initialized using a composite literal expressions, as well as with make().

https://www.godesignpatterns.com/2014/04/new-vs-make.html