package configLoad import ( "github.com/spf13/viper" "log" "sync" ) var defaultConfigFile = "./config.yaml" var configByYaml *viper.Viper var once sync.Once func LoadConfig() *viper.Viper { once.Do(func() { configByYaml = loadConfigFromYaml(defaultConfigFile) }) return configByYaml } func loadConfigFromYaml(path string) *viper.Viper { //log.Printf("读取配置文件:%s", path) vp := viper.New() vp.SetConfigFile(path) err := vp.ReadInConfig() if err != nil { log.Fatalf("Error while reading config file %s", err) } return vp }