August 2019
Beginner to intermediate
798 pages
17h 2m
English
There is a chance that you have a Go program that already uses the flag package and that you want to convert to using the viper package. Let us say that we have the following Go code that uses the flag package:
package main
import (
"flag"
"fmt"
)
func main() {
minusI := flag.Int("i", 100, "i parameter")
flag.Parse()
i := *minusI
fmt.Println(i)
}
The new version of that program, which is going to be saved in flagToViper.go, is going to use the viper package and will be presented in three parts. The first part of flagToViper.go is as follows:
package main
import (
"flag"
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
You will need to import the pflag package in order to work with command-line arguments in
Read now
Unlock full access