用GO语言对切片,通道和地图进行分配和直接分配有什么区别
问题描述:
I'm new to go language, look this code section
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?
我是新来的语言,请查看此代码段 p>
a:= make(map [string] string,10) a [“ name”] =“ Blob” //或 b:= map [string] string {} b [“ name”] = “ Blob” code> pre>问题: p>
“ make”是否在 p>
“ make”函数是否仅在初始化操作中增加了一步? 像C语言中的malloc和memset的组合? p> blockquote> blockquote> div>
答
The difference is that make(map[string]string, 10)
provides a capacity hint for the map and the composite literal map[string]string{}
does not.
The maps are allocated on the heap in both cases.
The make
function allocates and initializes an object as does the composite literal.