在 effective go 里写道:
new
https://golang.org/doc/effect...
does not initialize the memory,it only zeros it
make
https://golang.org/doc/effect...
It creates slices, maps, and channels only, and it returns an initialized (not zeroed) value of type T (not *T)
意思大概是:
new : allocate memory + zero it
make : allocate memory + initialize memory
我的疑问是:
Q1 : alloate memory 我能理解,initialize memory 是什么意思?
Q2 : 如上面写的 new zero it
,但 make not zeroed
,这个说法准确吗?好像仅 effective go 里提到了这个 zero 操作的区别,其他关于 new 和 make 区别的文章里都没有提到。
new:
new 返回一个指针,指向的对象是 zero value 。map slice channel 的 zero value 是 nil。
make 返回一个相应类型的对象(不是指针),这个对象一定不是 nil 。