5.2 Go语言中的内存分配

Go语言中有自动垃圾回收的机制(garbage collection),不需要为内存回收担心。而内存分配的有两种操作方式,new和make,本节重点讲述这两种操作方式。

new

内置函数new与其他语言类似,new(T)为T类型分配一个零空间,并返回其地址,即*T类型的值。或者换句话说,它返回一个指向新分配的T类型零值的指针,这一点需要牢记。

bytes.Buffer 的文档指出“Buffer 的零值是一个可以使用的空缓冲区”。 同样,sync.Mutex没有显式构造函数或Init方法。 相反,sync.Mutex 的零值被定义为未锁定的互斥锁。

参数定义

func new(Type) *Type

The new built-in function allocates memory. The first argument is a type, not a value, and the value returned is a pointer to a newly allocated zero value of that type.

make

内置的make(T)仅用于创建slices, maps和channels,返回类型T的初始化值(非零!),并且不是一个指针*T。产生这种区别的原因,是在使用前引用的数据类型必须进行初始化。

参数定义

func make(t Type, size …IntegerType) Type

The make built-in function allocates and initializes an object of type slice, map, or chan (only). Like new, the first argument is a type, not a value. Unlike new, make’s return type is the same as the type of its argument, not a pointer to it. The specification of the result depends on the type:

Slice: The size specifies the length. The capacity of the slice is equal to its length. A second integer argument may be provided to specify a different capacity; it must be no smaller than the length. For example, make([]int, 0, 10) allocates an underlying array of size 10 and returns a slice of length 0 and capacity 10 that is backed by this underlying array.
Map: An empty map is allocated with enough space to hold the specified number of elements. The size may be omitted, in which case a small starting size is allocated.
Channel: The channel’s buffer is initialized with the specified buffer capacity. If zero, or the size is omitted, the channel is unbuffered.

区别

new只负责分配,make负责初始化,区别点:

  • new(T)返回指针,*T指向零值T
  • make(T)返回初始化后的T
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老孙正经胡说

创作不易,感谢您的关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值