diff --git a/ch08/viper/myConfig.yaml b/ch08/viper/myConfig.yaml new file mode 100644 index 0000000..849d9fc --- /dev/null +++ b/ch08/viper/myConfig.yaml @@ -0,0 +1,5 @@ + item1: + k1: + - true + k2: + - myValue diff --git a/ch08/viper/myJSONConfig.json b/ch08/viper/myJSONConfig.json new file mode 100644 index 0000000..513f7de --- /dev/null +++ b/ch08/viper/myJSONConfig.json @@ -0,0 +1,13 @@ +{ + "item1": { + "key1": "val1", + "key2": false, + "key3": "val3" + }, + "item2": { + "key1": "val1", + "key2": true, + "key3": "val3" + } +} + diff --git a/ch08/viper/readJSON.go b/ch08/viper/readJSON.go index 9b101c5..15be996 100644 --- a/ch08/viper/readJSON.go +++ b/ch08/viper/readJSON.go @@ -7,17 +7,23 @@ import ( func main() { viper.SetConfigType("json") - viper.SetConfigFile("./myConfig.json") + viper.SetConfigFile("./myJSONConfig.json") fmt.Printf("Using config: %s\n", viper.ConfigFileUsed()) viper.ReadInConfig() if viper.IsSet("item1.key1") { - fmt.Println("item1.val1:", viper.Get("item1.key1")) + fmt.Println("item1.key1:", viper.Get("item1.key1")) } else { fmt.Println("item1.key1 not set!") } + if viper.IsSet("item2.key3") { + fmt.Println("item2.key3:", viper.Get("item2.key3")) + } else { + fmt.Println("item2.key3 is not set!") + } + if !viper.IsSet("item3.key1") { - fmt.Println("item3.key1 not set!") + fmt.Println("item3.key1 is not set!") } } diff --git a/ch08/viper/readYAML.go b/ch08/viper/readYAML.go index 11b0cf4..d01520c 100644 --- a/ch08/viper/readYAML.go +++ b/ch08/viper/readYAML.go @@ -30,10 +30,10 @@ func main() { } fmt.Printf("Using config: %s\n", viper.ConfigFileUsed()) - if viper.IsSet("item1.key1") { - fmt.Println("item1.val1:", viper.Get("item1.key1")) + if viper.IsSet("item1.k1") { + fmt.Println("item1.val1:", viper.Get("item1.k1")) } else { - fmt.Println("item1.key1 not set!") + fmt.Println("item1.k1 not set!") } if viper.IsSet("item1.k2") { @@ -42,7 +42,7 @@ func main() { fmt.Println("item1.k2 not set!") } - if !viper.IsSet("item3.key1") { - fmt.Println("item3.key1 not set!") + if !viper.IsSet("item3.k1") { + fmt.Println("item3.k1 is not set!") } }