当前位置:网站首页>深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
2022-04-23 18:33:00 【Ch3n】
在 Go 语言中,有两个比较雷同的内置函数,分别是 new
和 make
方法,其主要用途都是用于分配相应类型的内存空间。
看上去 new
和 make
都是分配内存的,那他们有什么区别呢?这个细节点也成为了不少 Go 语言工程师的面试题之一,值得大家一看。
在这篇文章中我们将来解答这个问题。
基本特性
make
在 Go 语言中,内置函数 make
仅支持 slice
、map
、channel
三种数据类型的内存创建,其返回值是所创建类型的本身,而不是新的指针引用。
函数签名如下:
func make(t Type, size ...IntegerType) Type
具体使用示例:
func main() {
v1 := make([]int, 1, 5)
v2 := make(map[int]bool, 5)
v3 := make(chan int, 1)
fmt.Println(v1, v2, v3)
}
在代码中,我们分别对三种类型调用了 make
函数进行了初始化。你会发现有的入参是有多个长度指定,有的没有。
这块的区别主要是长度(len) 和 容量(cap) 的指定,有的类型是没有容量这一说法,因此自然也就无法指定。
输出结果:
[0] map[] 0xc000044070
有一个细节点要注意,调用 make
函数去初始化 切片(slice) 的类型时,会带有零值,需要明确是否需要。
见过不少的小伙伴在这上面踩坑。
new
在 Go 语言中,内置函数 new
可以对类型进行内存创建和初始化。其返回值是所创建类型的指针引用,与 make
函数在实质细节上存在区别。
函数签名如下:
func new(Type) *Type
具体使用示例:
type T struct {
Name string
}
func main() {
v := new(T)
v.Name = "煎鱼"
}
从上面的例子的效果来看,是不是似曾相似?其实与下面这种方式的一样的:
func main() {
v := T{
}
v.Name = "煎鱼"
}
输出结果均是:
&{
Name:煎鱼}
其实 new
函数在日常工程代码中是比较少见的,因为他可被替代。
一般会直接用快捷的 T{}
来进行初始化,因为常规的结构体都会带有结构体的字面属性:
func NewT() *T {
return &T{
Name: "煎鱼"}
}
这种初始化方式更方便。
区别在哪里
可能会有的小伙伴会疑惑一点,就是 new
函数也能初始化 make
的三种类型:
v1 := new(chan bool)
v2 := new(map[string]struct{
})
那 make
函数的区别,优势是什么呢?
本质上在于 make 函数在初始化时,会初始化 slice
、chan
、map
类型的内部数据结构,new
函数并不会。
例如:在 map
类型中,合理的长度(len)和容量(cap)可以提高效率和减少开销。
更进一步的区别:
make
函数:- 能够分配并初始化类型所需的内存空间和结构,返回引用类型的本身。
- 具有使用范围的局限性,仅支持
channel
、map
、slice
三种类型。 - 具有独特的优势,make 函数会对三种类型的内部数据结构(长度、容量等)赋值。
new
函数:- 能够分配类型所需的内存空间,返回指针引用(指向内存的指针)。
- 可被替代,能够通过字面值快速初始化。
来源 脑子进煎鱼了 公众号
版权声明
本文为[Ch3n]所创,转载请带上原文链接,感谢
https://ch3nnn.blog.csdn.net/article/details/124290432
边栏推荐
- Using transmittablethreadlocal to realize parameter cross thread transmission
- CISSP certified daily knowledge points (April 18, 2022)
- 昇腾 AI 开发者创享日全国巡回首站在西安成功举行
- NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins
- In win10 system, all programs run as administrator by default
- logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
- Ctfshow - web362 (ssti)
- CISSP certified daily knowledge points (April 13, 2022)
- Const keyword, variable and function are decorated with const
- 线上怎么确定期货账户安全的?
猜你喜欢
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
MATLAB小技巧(6)七种滤波方法比较
【数学建模】—— 层次分析法(AHP)
硬核解析Promise對象(這七個必會的常用API和七個關鍵問題你都了解嗎?)
Use of regular expressions in QT
os_authent_prefix
Win1远程出现“这可能是由于credssp加密oracle修正”解决办法
视频边框背景如何虚化,简单操作几步实现
Solution to Chinese garbled code after reg file is imported into the registry
随机推荐
CISSP certified daily knowledge points (April 18, 2022)
Daily network security certification test questions (April 13, 2022)
CISSP certified daily knowledge points (April 15, 2022)
os_authent_prefix
After CANopen starts PDO timing transmission, the heartbeat frame time is wrong, PDO is delayed, and CANopen time axis is disordered
ctfshow-web362(SSTI)
硬核解析Promise對象(這七個必會的常用API和七個關鍵問題你都了解嗎?)
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
listener.log
CISSP certified daily knowledge points (April 12, 2022)
Halo open source project learning (VII): caching mechanism
Daily CISSP certification common mistakes (April 11, 2022)
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
CANopen usage method and main parameters of object dictionary
STM32学习记录0008——GPIO那些事1
Const keyword, variable and function are decorated with const
使用晨曦记账本,分析某个时间段每个账户收支结余
In win10 system, all programs run as administrator by default
【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
串口调试工具cutecom和minicom