Im new to go, and im using viper do load all my config what currently i have is the YAML look like below
countryQueries:
sg:
- qtype: gmap
qplacetype: postal_code
- qtype: gmap
qplacetype: address
- qtype: geocode
qplacetype: street_address
hk:
- qtype: gmap
qplacetype: postal_code
- qtype: gmap
qplacetype: address
- qtype: geocode
qplacetype: street_address
to note that the countrycode are dynamic it could be added anytime for any countries. So how do i map this to a struct where technically speaking i can do
for _, query := range countryQueries["sg"] { }
i try to contruct it myself by looping it but im stucked here
for country, queries := range viper.GetStringMap("countryQueries") {
// i cant seem to do anything with queries, which i wish to loop it
for _,query := range queries {} //here error
}
Any help will be much appreciated
Here some simple code with go-yaml:
After done some reading realized that viper has it own unmarshaling capabilities which works great https://github.com/spf13/viper#unmarshaling
So here what i did
You can use this package https://github.com/go-yaml/yaml to serialize/deserialize YAML in Go.
If you're looking to map your
yaml
structure to a strict golangstruct
you could make use of the mapstructure library to map the nested key/value pairs under each country.For example: