Cannot assign to struct field in a map

For those looking for a simpler example:

This is wrong:

type myStruct struct{
   Field int
}

func main() {
   myMap := map[string]myStruct{
        "key":{
            Field: 1,
        },
   }

   myMap["key"].Field = 5
}

Because myMap["key"] is not "addresable".

This is correct:

type myStruct struct{
   Field int
}

func main(){
   myMap := map[string]myStruct{
       "key":{
           Field: 1,
       },
   }

   // First we get a "copy" of the entry
   if entry, ok := myMap["key"]; ok {

       // Then we modify the copy
       entry.Field = 5
    
       // Then we reassign map entry
       myMap["key"] = entry
   }

   // Now "key".Field is 5
   fmt.Println(myMap) // Prints map[key:{5}]
}

Here you have a working example.


First, for this question, the solution in this post Why do I get a "cannot assign" error when setting value to a struct as a value in a map? works perfectly fine.

Then, finally figured out why after I already changed to use pointer my case still doesn't work, refer to the below very simple code:

a := make([]int, 3)
fmt.Println(len(a))

b := make(map[string]string, 3)
fmt.Println(len(b))

What do think the output will be? I simply thought it is all would be: 3, but actually for the map, the output will be 0

Then later in the map initialization process, i used a for loop and with this value len(snapshots), that means the initialization process will never get run...

Yea, that is the reason.


What I ended up doing to use my struct map in a loop was the following:

type testStruct struct {
  a string
  b int
}

func main() {
  mapTest := make(map[string]testStruct)
  abc := [3]string{"a", "b", "c"}

  for i := 0; i < len(abc); i++ {
    var temp testStruct
    temp.a = abc[i]
    temp.b = i
    mapTest[abc[i]] = temp
  }

  fmt.Println(mapTest)
}

Output should be:

map[b:{b 1} c:{c 2} a:{a 0}]

It's not appending, but should work to assign multiple values to a struct map, alternatively you could do the following and allow the map to reference its own values:

func main() {
  mapTest := make(map[string]testStruct)

  abc := [3]string{"a", "b", "c"}
  for i := 0; i < len(abc)*2; i++ {
    temp := mapTest[abc[i%3]]
    temp.a = abc[i%3]
    temp.b = temp.b + i
    mapTest[abc[i%3]] = temp
  }

  fmt.Println(mapTest)
}

Which should output:

map[a:{a 3} b:{b 5} c:{c 7}]

Note that no errors are raised when we reference an empty struct value, this is because when we initialize our struct, its values start out as empty values but not nil (0 for int, "" for string, etc.)