mapの値を更新したい場合
Goでmapの値を更新したい場合
次のようにmapへ直接更新することはできない
type Sample struct { state bool } var samples = map[string]Sample{ "hoge": {true}, "fuga": {false}, } fmt.Println(samples["hoge"].state) // getはできる samples["hoge"].state = false // setはできない
https://play.golang.org/p/8ZobtXRtw86
次のように更新した値でmapの内容を書き換える必要がある。
type Sample struct { state bool } var samples = map[string]Sample{ "hoge": {true}, "fuga": {false}, } s := samples["hoge"] s.state = false samples["hoge"] = s // 更新したもので置き換える fmt.Println(samples)