bmw0128
bmw0128

Reputation: 13698

Initializing map as a field in a Go struct

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"

Upvotes: 0

Views: 88

Answers (1)

OneOfOne
OneOfOne

Reputation: 99274

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

Upvotes: 2

Related Questions