You cannot assign a struct field off map directly in golang

Software development in emerging markets

Published on 29 Aug 2013


package main
import “fmt”
var m = map[string]struct{x, y int} {
	“foo”: {2, 3},
}
func main() {
	m[“foo”].x = 4 // cannot assign to m[“foo”].x
	fmt.Println(m)
}

Here’s above snippet at Go Playground

This issue #3117 is still not resolved.

The work around is to make the map to refer to a pointer of struct.


package main
import “fmt”
var m = map[string]*struct{x, y int} {
	“foo”: {2, 3},
}
func main() {
	m[“foo”].x = 4
	fmt.Println(m[“foo”].x)
}

You can check above snippet at Go Playground