Good day for all readers!
I have api service which needs key for some decrypt issues. Key calculates all time when I make request. That is wrong from an architectural point of view.
The option with environments (.env, viper...) drops because, in the future, I want to bring out this module to a separate library.
Hint me, how it is possible without using environments to calculate the key when starting the application and use it until stopping the application?
CodePudding user response:
If you are sure you need a global variable, you can do this:
package main
var globalVar string = "My string"
func init(){
globalVar = "The new value of the variable"
}
func main(){
println(globalVar)
}
CodePudding user response:
A global var starts with a capital, so it is reachable from within other packages.
But... it's actually bad behaviour, because those other packages can change it.
Having a non capital one, still means that functions in the same package can change it.
Perhaps you can use a constant?
or make a singleton variable, see :