go - GO语言中slice,channel,map的赋值和直接赋值有什么区别

标签 go

我是 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/

相关文章:

multithreading - Golang 中的 channel 和 mutex 有什么区别?

json - 如何解析来自 AWS Go API 的响应

json - Golang 将 JSON 解码为 protobuf 生成的结构

javascript - 将 Javascript 变量的值发送到 Go 变量中

mongodb - 使用 golang 从已经运行的集群的主实例重播另一个 mongodb 实例中的 oplog

docker - Go的时间从头开始在docker镜像下不起作用

Golang 解码 JSON 响应,然后使用 Struct 字段名称编码

go - 带有/代理中间件的 Echo CORS 会导致/Access-Allow-Origins 响应 header 出现问题

go - 如何将结构作为参数传递给函数?

datetime - 从Win32时代开始将纳秒添加到Go时间