当前位置:网站首页>深入理解 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
边栏推荐
- After CANopen starts PDO timing transmission, the heartbeat frame time is wrong, PDO is delayed, and CANopen time axis is disordered
- RC smart pointer in rust
- Halo open source project learning (VII): caching mechanism
- ESP32 LVGL8. 1 - label (style 14)
- Pointers in rust: box, RC, cell, refcell
- Daily CISSP certification common mistakes (April 11, 2022)
- 串口调试工具cutecom和minicom
- Solution to Chinese garbled code after reg file is imported into the registry
- Can filter
- Excel intercept text
猜你喜欢

Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash

Dynamically add default fusing rules to feign client based on sentinel + Nacos

【ACM】70. 爬楼梯

【ACM】509. Fibonacci number (DP Trilogy)

Stm32mp157 wm8960 audio driver debugging notes

Halo 开源项目学习(七):缓存机制

In win10 system, all programs run as administrator by default

PowerDesigner various font settings; Preview font setting; SQL font settings

Robocode tutorial 3 - Robo machine analysis

机器学习理论之(8):模型集成 Ensemble Learning
随机推荐
Quantexa CDI(场景决策智能)Syneo平台介绍
教你用简单几个步骤快速重命名文件夹名
Gst-launch-1.0 usage notes
Test questions of daily safety network (February 2024)
纠结
14个py小游戏源代码分享第二弹
Halo 开源项目学习(七):缓存机制
STM32: LCD显示
Daily network security certification test questions (April 18, 2022)
Mysqldump backup database
Daily CISSP certification common mistakes (April 14, 2022)
PowerDesigner various font settings; Preview font setting; SQL font settings
Excel intercept text
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
Ionic 从创建到打包指令集顺序
【ACM】509. Fibonacci number (DP Trilogy)
Introduction to quantexa CDI syneo platform
Stm32mp157 wm8960 audio driver debugging notes
NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins