I am building a little app using Viper and Cobra. At the moment, I have a yaml file like this:
hosts:
- name: host1
port: 90
key: my_key
- name: host2
port: 90
key: prompt
And I've read in the config file using Viper.
When I run viper.Get("hosts")
it returns an interface (or a slice of interfaces?). This is the data structure I end up with:
([]interface {}) (len=2 cap=2) {
(map[interface {}]interface {}) (len=3) {
(string) (len=4) "name": (string) (len=20) "host1",
(string) (len=4) "port": (int) 90,
(string) (len=3) "key": (string) (len=6) "my_key"
},
(map[interface {}]interface {}) (len=3) {
(string) (len=3) "key": (string) (len=6) "prompt",
(string) (len=4) "name": (string) (len=20) "host2",
(string) (len=4) "port": (int) 90
}
}
What I'd like to do here is loop over each of the array elements and perform an operation using the values of name, port and key.
I'm completely new to interfaces in Golang, so this isn't very clear, and the literature on this is extremely confusing :(
Any help is appreciated.