viper.SetConfigName("config")// 配置文件名,不需要后缀名 viper.SetConfigType("yml")// 配置文件格式 viper.AddConfigPath("/etc/appname/")// 查找配置文件的路径 viper.AddConfigPath("$HOME/.appname")// 查找配置文件的路径 viper.AddConfigPath(".")// 查找配置文件的路径 err:=viper.ReadInConf...
// LoadConfig reads configuration from file or environment variables. func LoadConfig(path string) (config Config, err error) { // Read file path viper.AddConfigPath(path) // set config file and path viper.SetConfigName("app") viper.SetConfigType("env") // watching changes in app.env ...
这里的问题是.env中的键不正确。默认的键分隔符是.(参见源代码)。因此键应该是DATABASE.CONNECTIONURI...
To work with environment variables in Go, we can use the os package or the third-party godotenv or viper libraries. $ go version go version go1.22.2 linux/amd64 We use Go version 1.22.2. Go os.GetenvThe Getenv retrieves the value of the environment variable named by the key. It ...
viper.SetConfigName(".demo")// name of config file (without extension)viper.AddConfigPath("$HOME")// adding home directory as first search pathviper.AutomaticEnv()// read in environment variables that match// If a config file is found, read it in.iferr := viper.ReadInConfig(); err ...
viper.SetConfigName(".demo") } viper.AutomaticEnv()//read in environment variables that match//If a config file is found, read it in.iferr := viper.ReadInConfig(); err ==nil { fmt.Println("Using config file:", viper.ConfigFileUsed()) ...
viper.SetConfigName(".demo")// name of config file (without extension)viper.AddConfigPath("$HOME")// adding home directory as first search pathviper.AutomaticEnv()// read in environment variables that match// If a config file is found, read it in.iferr := viper.ReadInConfig(); err ...
(cfgFile) // } // viper.SetConfigName(".demo") // name of config file (without extension) // viper.AddConfigPath("$HOME") // adding home directory as first search path // viper.AutomaticEnv() // read in environment variables that match // // If a config file is found, read ...
viper.SetConfigName(".smartant-cli") } viper.AutomaticEnv()// read in environment variables that match // If a config file is found, read it in. iferr:=viper.ReadInConfig();err==nil{ fmt.Println("Using config file:",viper.ConfigFileUsed()) ...
Using Viper to load configuration from Files and Environment Variables is reasonably straightforward. But adding Consul is where things are a bit more confusing, especially if you’ve used Consul in the past. When I first attempted to use Viper with Consul, it took me quite a bit of time to...