-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can I get an array of tables using TOML? #213
Comments
This isn't a support forum. Please confer with the docs, or, maybe try the http://discuss.gohugo.io/ forum (Hugo uses viper). |
@bep tad harsh. I came here looking for the same thing since I couldn't see anything in the docs, I wouldn't think of going to hugo's support discuss for questions relating to this library. Anyway, incase anyone comes here for this, given this example: [lights]
address = "127.0.0.1"
[[lights.presets]]
relay = 1
name = "Night"
[[lights.presets]]
relay = 2
name = "Day"
[[lights.presets]]
relay = 3
name = "Low Intensity"
[[lights.presets]]
relay = 8
name = "Blackout" You can access it with a type assertion on presets, ok := viper.Get("lights.presets").([]map[string]interface{})
if !ok {
return nil, errors.New("incorrectly configured light presents")
} This will give you a slice of maps. The value of the map key is an |
presets, ok := viper.Get("lights.presets").([]map[string]interface{})
if !ok {
return nil, errors.New("incorrectly configured light presents")
} The type assert always be false. presets, ok := viper.Get("lights.presets").([]interface{}) // type assert to '[]interface{}'
if !ok {
return nil, errors.New("incorrectly configured light presents")
}else{
for _, table := range presets {
if m, ok := table.(map[string]interface{}); ok { // type assert here
fmt.Println(cast.ToInt(m["relay"])) // need cast, "github.com/spf13/cast"
}
}
} This could be ok. Another example: [[lights]]
relay = 1
name = "Night"
[[lights]]
relay = 2
name = "Day"
[[lights]]
relay = 3
name = "Low Intensity"
[[lights]]
relay = 8
name = "Blackout" Get presets, ok := viper.Get("lights").([]interface{})
if !ok {
return nil, errors.New("incorrectly configured light presents")
}else{
for _, table := range presets {
if m, ok := table.(map[string]interface{}); ok { // type assert here
fmt.Println(cast.ToInt(m["relay"])) // need cast, "github.com/spf13/cast"
// cast 'name' too
}
}
} |
Here is an example config file in TOML:
How can I get these values properly, so that I can iterate over them using something like a
for x := range y
statement, where y is a slice of users?The text was updated successfully, but these errors were encountered: