package myconf import ( "fmt" "github.com/spf13/pflag" "github.com/spf13/viper" ) var ( vp = viper.New() ) func LoadFlag() { err := vp.BindPFlags(pflag.CommandLine) if err != nil { panic(fmt.Errorf("load command line fail: %s", err)) } } func LoadFile(confFile string, conf interface{}) { vp.SetConfigFile(confFile) err := vp.ReadInConfig() if err != nil { panic(fmt.Errorf("read file fail: %s", err)) } err = vp.Unmarshal(conf) if err != nil { panic(fmt.Errorf("parse file fail: %s", err)) } } func Conf() *viper.Viper { return vp }