当前位置:网站首页>深入理解 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
边栏推荐
- RC smart pointer in rust
- Robocode tutorial 8 - advanced robot
- 【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))
- Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
- ctfshow-web361(SSTI)
- CISSP certified daily knowledge points (April 12, 2022)
- Custom prompt box MessageBox in QT
- 【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
- listener.log
- Ionic 从创建到打包指令集顺序
猜你喜欢
教你用简单几个步骤快速重命名文件夹名
From introduction to mastery of MATLAB (2)
Dynamically add default fusing rules to feign client based on sentinel + Nacos
Excel intercept text
iptables -L执行缓慢
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Use of regular expressions in QT
Function recursion and solving interesting problems
随机推荐
Serialization scheme of serde - trust
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
QT add external font ttf
14个py小游戏源代码分享第二弹
Kettle paoding jieniu Chapter 17 text file output
配置iptables
Log4j2 cross thread print traceid
Promote QT default control to custom control
回路-通路
Const keyword, variable and function are decorated with const
Cutting permission of logrotate file
C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
Robocode tutorial 8 - advanced robot
In win10 system, all programs run as administrator by default
Ionic instruction set order from creation to packaging
CISSP certified daily knowledge points (April 15, 2022)
Daily network security certification test questions (April 15, 2022)
listener. log
Introduction to quantexa CDI syneo platform
Interpretation and compilation of JVM