Can't seem to use append
for this case. Any help would be appreciated.
First argument to append
must be slice:
package main
import (
"fmt"
)
type C struct {
value5 string
value6 string
}
type B struct {
value3 string
value4 C
}
type A struct {
value1 string
value2 B
}
type X struct{
key int
}
func main() {
letSee := map[X]A{}
letSee[X{1}]=A{"T",B{"T1",C{"T11","T12"}}}
letSee[X{1}]=append(letSee[X{1}], A{"L",B{"L1",C{"L11","L12"}}})
fmt.Println(letSee)
}
If in a map you want to store multiple values associated with the same key, the value type must be suitable for that. A struct isn't, but a slice is a perfect choice.
So change your value type to []A
:
letSee := map[X][]A{}
letSee[X{1}] = []A{A{"T", B{"T1", C{"T11", "T12"}}}}
letSee[X{1}] = append(letSee[X{1}], A{"L", B{"L1", C{"L11", "L12"}}})
fmt.Printf("%+v", letSee)
Output (try it on the Go Playground):
map[{key:1}:[{value1:T value2:{value3:T1 value4:{value5:T11 value6:T12}}}
{value1:L value2:{value3:L1 value4:{value5:L11 value6:L12}}}]]