理解 Go make 和 new 的区别

三月沙 ·
Golang.org Slices, maps and channels are reference types that do not require the extra indirection of an allocation with new. The built-in function make takes a type T, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions. It returns a value of type T (not *T). The memory is initialized as described in the section on initial values Ref https://golang.org/ref/spec#Making_slices_maps_and_channels
#4
更多评论
slice map channel使用字面量literal的初始化方式 仍然类似于c++中的引用传递 传递后可以直接改变原始slice map或channel的值 引用传递和make的初始化方式应该是无关的 另外注意struct的var申明并非nil 而是带有初始化后的结构体对象 而slice map channel 的申明为nil
#1
Golang中的数组为值传递 且append操作slice的时候必须传入指针 否则原slice不会被append成功添加
#2