Skip to content

Instantly share code, notes, and snippets.

@danesparza
Last active June 12, 2017 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danesparza/756099e14b054390b3604b157b2c5ffb to your computer and use it in GitHub Desktop.
Save danesparza/756099e14b054390b3604b157b2c5ffb to your computer and use it in GitHub Desktop.
Testing using viper with lists in config
package main
import (
"bytes"
"fmt"
"github.com/spf13/viper"
)
func main() {
viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
// any approach to require this configuration into your program.
var yamlExample = []byte(`
Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
age: 35
eyes : brown
beard: true
`)
viper.ReadConfig(bytes.NewBuffer(yamlExample))
fmt.Println("Hobbies:")
keytoread := "hobbies"
if viper.InConfig(keytoread) {
items := viper.GetStringSlice(keytoread)
for _, item := range items {
fmt.Printf("%v\n", item)
}
}
fmt.Println("Clothing:")
keytoread = "clothing"
if viper.InConfig(keytoread) {
items := viper.GetStringMapString(keytoread)
for k, v := range items {
fmt.Printf("%v : %v\n", k, v)
}
}
}
@danesparza
Copy link
Author

Updated example. Included examples for both lists (slices) and maps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment