Last active
June 12, 2017 12:42
-
-
Save danesparza/756099e14b054390b3604b157b2c5ffb to your computer and use it in GitHub Desktop.
Testing using viper with lists in config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated example. Included examples for both lists (slices) and maps