I am using viper for reading config files in my go module.
Tree structure;
.
├── README.md
├── package1
│ └── package1.go
├── config.json
├── helper
│ └── helper.go
└── main.go
I initialized Viper in my main.go
viper.SetConfigFile("config.json")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
Now I can get keys with viper.GetString("key")
, everything works fine.
But if In package1.go I do viper.GetString("key")
I get empty string(no value).
I had to initialize Viper in package1.go
with SetConfigFile
and AddConfigPath
to make it work.
I dont feel this is the right way, what am I doing wrong?