0
package main

import (
    "fmt"
)

func main() {
    a := make(map[string][3]float64)
    a["test"] = [3]float64{0.0}
    a["test"][0] = 1.0
    fmt.Printf("%v", a["test"])
}

Here is the playground demo, which returns error

tmp/sandbox665292783/main.go:9: cannot assign to a["test"][1]

If I print the grammar of a["test"], it returns an array of len 3.

Junchen
  • 1,749
  • 2
  • 18
  • 25
  • Assigning to an item in an array changes the value; it's like assigning to a struct field, discussed here: https://stackoverflow.com/questions/32751537/why-do-i-get-a-cannot-assign-error-when-setting-value-to-a-struct-as-a-value-i/32751792#32751792 – twotwotwo Aug 22 '17 at 01:00
  • 1
    To avoid the error, you either need to change the map value type (to []float64 or *[3]float64, for example) or construct a whole new `[3]float` to put back in the map (e.g. by explicitly copying the current value to a temp variable, modifying that, and assigning it back). – twotwotwo Aug 22 '17 at 01:02
  • thank you for pointing me to the right direction! – Junchen Aug 22 '17 at 01:18

0 Answers0