I've been trying to update a map variable inside a struct but with no luck.
I expected that maps in Golang are passed by reference and hence assigning one reference to another should work but that's not the case.
I might be missing something obvious here, and if that's the case, apologies!
package main
import (
"fmt"
)
type Foo struct {
t map[string]interface{}
}
func (F Foo) Set(ta map[string]interface{}) {
F.t = ta
}
func (F Foo) Get() map[string]interface{} {
return F.t
}
func main() {
t := make(map[string]interface{})
t["t"] = "sf"
t["array"] = []int{1, 2, 3}
fmt.Println(t) // prints map[t:sf array:[1 2 3]]
var f Foo
f.Set(t)
// why the following?
fmt.Println(f.t) //prints map[]
f.t = t
fmt.Println(f.t) //prints map[t:sf array:[1 2 3]]
}
Playground: https://play.golang.org/p/i1ESV1BdjGQ