// 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.SetC
这里的问题是.env中的键不正确。默认的键分隔符是.(参见源代码)。因此键应该是DATABASE.CONNECTIONURI...
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")// 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(".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...
(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 ...
使用env文件时Golang数据库/SQL无法连接字符串 这将不起作用,因为变量在init运行之前被初始化;这是根据规范:一个没有导入的包的初始化方法是将初始值赋给它的所有包级变量,然后按照它们在源代码中出现的顺序调用所有init函数,可能是在多个文件中,就像提交给编译器一样。如果一个包有导入,导入的包在初始化包...
= nil {// fmt.Println(err)// os.Exit(1)// }/// // Search config in home directory with name ".cobra-demo" (without extension).// viper.AddConfigPath(home)// viper.SetConfigName(".cobra-demo")// }/// viper.AutomaticEnv() // read in environment variables that match/// // ...