Issue on loading the configuration on file scope global variable.
The config setting and and loading config values inside main method
func main() {
if err := config.LoadConfig(); err != nil {
logrus.Info("Unable to read config : ")
os.Exit(-1)
}
fmt.Println(config.GetConfig().Value) // print the correct value
service.Test() // prints 0
}
my config setting. config.go
var appConfig AppConfig
func LoadConfig() error {
// loads config
appConfig = ....
}
func GetConfig() AppConfig {
return appConfig
}
I have another service file with global variable defined inside service.go
var x = config.GetConfig().Value
func Test(){
fmt.Println(x)
}
Question:
Why my global variable x inside service.go is not initialized ?
Thanks