-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathyaml.go
46 lines (42 loc) · 893 Bytes
/
yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"bytes"
"github.com/spf13/viper"
"log"
)
func main() {
//
// Initialize from our config file
//
//viper.SetConfigType("yaml")
//viper.SetConfigName("yaml-config") // name of config file (without extension)
//viper.AddConfigPath(".")
//viper.WatchConfig()
//err := viper.ReadInConfig() // Find and read the config file
//if err != nil {
//log.Fatalln(err)
//}
// Parse bytes
var yamlExample = []byte(`
Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
age: 35
eyes : brown
beard: true
Hacker: false
`)
yaml := viper.New()
yaml.SetConfigType("yaml")
yaml.ReadConfig(bytes.NewBuffer(yamlExample))
log.Println("clothing.jacket", yaml.Get("clothing.jacket"))
log.Println("age", yaml.Get("age"))
log.Println("hobbies", yaml.Get("hobbies"))
log.Println("hacker", yaml.Get("Hacker"))
}