Open ChristianAE opened 8 years ago
I am facing the same issue. This is preventing me from modularizing my code.
One approach is to make your config setter return an instance of viper, then use that returned object whenever you want to read the config. However if you are concerned about reading the config file multiple times when calling the config setter, the factory pattern would be useful. Eighter viper.GetViper and viper.New returns instances of viper and sync.Once is beneficial for the factory patter.
I've come here with the same question as the topic starter. As the problem is solved even with two different aproaches, probably it's a time to close this issue? @ChristianAE What do you think about it?
In my main.go file I placed the code to read the config file. In the main package (main.go) I can read from the config without any issues. When I use the same code in a different package (storage/storage.go) the value is empty. Is there a way to make the config read in the main package available to other packages?
Reading the config:
Reading from the config:
port := viper.GetString("port")
Structure of the project:
config.json: