August 2019
Beginner to intermediate
798 pages
17h 2m
English
Before going into more advanced examples, I will present some sample Go code that uses viper. The name of the program is usingViper.go and it will be presented in three parts.
The first part of useViper.go is as follows:
package main
import (
"fmt"
"github.com/spf13/viper"
)
The second part of useViper.go is as follows:
func main() {
viper.BindEnv("GOMAXPROCS")
val := viper.Get("GOMAXPROCS")
fmt.Println("GOMAXPROCS:", val)
viper.Set("GOMAXPROCS", 10)
val = viper.Get("GOMAXPROCS")
fmt.Println("GOMAXPROCS:", val)
The last part of useViper.go contains the following Go code:
viper.BindEnv("NEW_VARIABLE") val = viper.Get("NEW_VARIABLE") if val == nil { fmt.Println("NEW_VARIABLE not defined.") return } fmt.Println(val) ...Read now
Unlock full access