golang中new和make的区别

1. 先看一个例子

package main

import "fmt"

func main() {
    var a *int
    *a = 10
    fmt.Println(*a)
}

运行结果是啥呢?
image.png

问:为什么会报这个panic呢?
答:因为如果是一个引用类型,我们不仅要声明它,还要为它分配内存空间,否则我们赋值的 10 就无处安放。值类型的声明不需要我们分配内存空间,因为已经默认给我们分配好啦。

问:那么如何分配内存呢?
答:在使用a这个引用类型的变量之前给它分配内存内存即可。

package main

import "fmt"

func main() {
    var a *int
    a = new(int)
    *a = 10
    fmt.Println(*a)
}

问:听说对变量分配内存有new和make函数两种,我应该在对各种变量初始化和分配内存时用哪个呢?
答:在golang中的源码中已经做出了解释可以接着看后面的内容。

2. 源码中的描述

make函数用来对slicemap、和channel类型的初始化和分配内存。和new函数一样,第一个参数是一个类型而不是值;和new函数不一样的是make函数的返回类型和传入参数的类型一致,而new函数的返回类型是传入类型的指针。特定的结果依赖于传入的类型:

  • slice:size指定切片的长度,容量在不指定的情况下和长度相等。如果额外指定容量则容量可能和长度不相等。比如make([]int,0,10)分配了一个大小为10的底层数组,并返回一个长度为0、容量为10的切片,该切片由这个底层数组支持。
  • map:一个空的map被分配了足够的空间以容纳指定数量的元素。如果省略了大小,则会分配一个较小的起始大小。
  • 通道的缓冲区使用指定的缓冲区容量进行初始化。如果容量为0或者未指定大小,则通道是无缓冲的。

new函数用于分配内存。其第一个参数是一个类型,而不是一个值,返回的值是指向新分配的内存的指针,该内存的内容是该类型的零值。

// 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.
func make(t Type, size ...IntegerType) 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.
func new(Type) *Type

3. 两者的异同

相同点:

  1. new 和 make 都是用于内存的分配(堆上)。
  2. 用于给类型分配内存空间,并且变量的值为零。

不同点:

  1. make 只用于 chan,map,slice 的初始化。
  2. make 返回类型本身,new 返回指向类型的指针。
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值