我是golang的新手,在运行此代码片段时,我将len设置为4,试图理解为什么会这样?
package main
import "fmt"
type phone struct {
model string
camera Camera
ram int
}
type Camera struct {
lens string
aparature int
}
func main() {
var m = make(map[string]phone)
myphn1 := phone{model: "iphone", camera: Camera{"20", 4}, ram: 6}
myphn2 := phone{model: "pixel", camera: Camera{"50", 2}, ram: 6}
m["myphn1"] = myphn1
m["myphn2"] = myphn2
var k = make([]string, len(m))
for key, _ := range m {
k = append(k, key)
}
fmt.Println(k)
fmt.Println(len(k))
}
我知道这在创建时增加了2的大小,但是在打印时它给出了类似的东西,是2个未分配条目的答案空间吗?
[ myphn2 myphn1]
4
慕娘9325324
慕田峪7331174
相关分类