79282725

Date: 2024-12-15 16:31:40
Score: 1.5
Natty:
Report link

it looks like we have the same problem,for some project that i have sometimes it worked sometimes not ,for some my project im call the function in the root app

config/config.go

func InitConfig() {
    viper.SetConfigName("config")
    viper.SetConfigType("yaml")
    viper.AddConfigPath(".")
    err := viper.ReadInConfig()
    if err != nil {
        panic(fmt.Errorf("fatal error config file: %w", err))
    }
    replacer := strings.NewReplacer(".", "_")
    viper.SetEnvKeyReplacer(replacer)
    viper.AutomaticEnv()
}

cmd/root.go(or just call it in your main.go)

   func initConfig(){
        config.InitConfig()
    }

    func Execute(){
        initConfig()
        if err := rootCmd.Execute(); err != nil {
            log.Fatal(err)
        }
  }
Reasons:
  • Whitelisted phrase (-1): it worked
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Me too answer (2.5): have the same problem
  • Low reputation (1):
Posted by: Kanisius X-1