英文:
Initializing map as a field in a Go struct
问题
我有:
type Foo struct{
Name string
Hands map[string]string
}
aFoo := Foo{
Name: "Henry",
Hands: ???????
}
我想为 "Hands" 设置一些值,但是我无法正确使用语法。
例如,我想使用一个类似于以下的映射:
"Left":"broken"
"Right":"missing thumb"
英文:
I have:
type Foo struct{
Name string
Hands map[string]string
}
aFoo := Foo{
Name: "Henry"
Hands: ???????
}
I want to set some values for "Hands", but I cannot get the syntax correct.
As an example, I want to use a map like:
"Left":"broken"
"Right":"missing thumb"
答案1
得分: 2
Foo{
Name: "Henry",
Hands: make(map[string]string),
}
aFoo.Hands["Left"] = "broken"
// 或者只是
Foo{
Name: "Henry",
Hands: map[string]string{"Left": "broken", "Right": "missing thumb"},
}
英文:
Foo{
Name: "Henry",
Hands: make(map[string]string),
}
aFoo.Hands["Left"] = "broken"
// or just
Foo{
Name: "Henry",
Hands: map[string]string{"Left": "broken", "Right": "missing thumb"},
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论