I tried many ways to build a map of struct and append values to it and I did not find any way to do it.
The keys of the map are strings. The struct is made of two parts : "x" integer and "y" a slice of strings.
The errors I face to build the map are (for m) : main.go:11: syntax error: unexpected comma, expecting semicolon, newline, or }
When I try to add new keys and values to the map, the errors are : go:33: syntax error: missing operand
Thank you very much to help me find the mistakes !
package main
import "fmt"
type TEST struct {
x int
y []string
}
// none of these var gives the expected result
var m = map[string]*struct{x int, y []string}{"foo": {2, {"a", "b"}}, }
var m2 = map[string]struct{x int, y []string}{"foo": {2, {"a", "b"}}, }
var n = map[string]*struct{x int
y []string
}{"foo": {2, {"a", "b"}}, }
var o = map[string]*struct{
x int
y []string
}{"foo": {2, {"a", "b"}}, }
func main() {
m["foo"].x = 4
fmt.Println(m["foo"].x)
// how can I had a new key ?
m["toto"].x = 0
m["toto"] = {0, {"c", "d"}}
// and append the string "e" to {"c", "d"} ?
m["toto"].y = append(m["toto"].y, "e")
a := new(TEST)
a.x = 0
a.y = {"c", "d"}
m["toto"] = a
fmt.Println(m)
}