I'm new to Golang, and I'm trying to initialize a struct of structs with a list of strings at the bottom. I'd like to be able to access the values inside in a clean and readable way, but it isn't clicking.
for example, the struct setup is as follows:
type config struct {
// a config can have many replicas
configName string
replicas []string
}
type instance struct {
// an instance can have many configs
name string
configs []config
}
type user struct {
// a user can have up to two instances
instances []instance
}
And I'm initializing it like so:
var users = map[string][]user{
"user-1": []user {
{
[]instance {
{
"instance-1",
[]config {
{
"some-config-1",
[]string{"xx", "yy", "zz",},
},
},
},
},
},
},
"user-2": {
{
[]instance {
{
"instance-2",
[]config {
{
"some-config-2",
[]string{"xx", "yy", "zz",},
},
},
},
},
},
},
}
Then, to access any of the fields, I have to do gross stuff like:
func main() {
for user, _ := range users {
fmt.Println(users[user][0].instances[0].name)
}
}
There must be a better way? Running (but gross) code example here: http://play.golang.org/p/9M8pyBnMPB