Golang new()和make()

1、func new(Type) *Type

new 为一个 T 类型的值分配了内存空间,并将此空间初始化为该类型的零值,返回的是内存空间的地址,即该类型的指针。

*t指向的内容的值为零(zero value),并不是指针为零。

2、func make(t Type, size ...IntegerType) Type

make(t, args)与new(t)的功能区别是,make只能创建slice、map和channel,

并且返回一个初始化的(而不是置零),类型为t的值(而不是*t)。

猜你喜欢

转载自blog.csdn.net/hengchi_hengchi/article/details/120460127