将map初始化为Go结构中的字段

时间:2014-05-08 02:17:26

标签: go

我有:

type Foo struct{
  Name string
  Hands map[string]string
}

aFoo := Foo{
  Name: "Henry"
  Hands: ???????
}

我想为" Hands"设置一些值,但我无法正确理解语法。 例如,我想使用如下地图:

"Left":"broken"
"Right":"missing thumb"

1 个答案:

答案 0 :(得分:2)

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"},
}