我是 go 语言的新手,请看这段代码
a := make(map[string]string, 10)
a["name"] = "Blob"
// or
b := map[string]string{}
b["name"] = "Blob"
Questions:
Does "make" allocate memory on heap?
Does the "make" function only add one step to the initialization operation? like combination of malloc and memset in C language?
最佳答案
不同之处在于 make(map[string]string, 10)
为 map 提供容量提示,而复合文字 map[string]string{}
提供容量提示不是。
在这两种情况下,映射都分配在堆上。
make
函数分配和初始化一个对象,就像复合文字一样。
关于go - GO语言中slice,channel,map的赋值和直接赋值有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43691791/