Go语言中的内存分配
Go语言中有自动垃圾回收的机制(garbage collection),不需要为内存回收担心 。而内存分配的有两种操作方式,new和make,本节重点讲述这两种操作方式 。
new内置函数new与其他语言类似,new(T)为T类型分配一个零空间,并返回其地址,即*T类型的值 。或者换句话说,它返回一个指向新分配的T类型零值的指针,这一点需要牢记 。
【Go语言中的内存分配】bytes.Buffer 的文档指出“Buffer 的零值是一个可以使用的空缓冲区” 。同样,sync.Mutex没有显式构造函数或Init方法 。相反,sync.Mutex 的零值被定义为未锁定的互斥锁 。
参数定义
func new(Type) *Typemake内置的make(T)仅用于创建slices, maps和channels,返回类型T的初始化值(非零!),并且不是一个指针*T 。产生这种区别的原因,是在使用前引用的数据类型必须进行初始化 。
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.
参数定义
func make(t Type, size ...IntegerType) Type区别new只负责分配,make负责初始化,区别点:
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(T)返回指针,*T指向零值T
- make(T)返回初始化后的T
推荐阅读
- 你知道有哪些计算机语言吗 计算机语言有哪些
- LPDDR5|iPhone 14或全系6G内存:标准版依然精准阉割、内存规格差一代!
- 笑靥如花中的靥是指? 笑靥如花什么意思
- 苹果|内存大升级!iPhone 14搭载A16性能预测:苹果又要默秒全安卓阵营?
- iPhone|iPhone 14或全系6GB RAM:Pro/Pro Max升级LPDDR5内存
- Bingo这个词在口语中的五大用法 bingo是什么意思
- 精卫填海中的精卫是什么性格 精卫填海中的精卫是什么
- 生活中的立体图形 立体图形有哪些
- |职场中的情况一般是相当复杂的
- tt是什么意思 医学类 tt是什么意思