Answer the question
In order to leave comments, you need to log in
How to make a variable global to all packages in golang?
Addendum to this question
Here I have created a global variable for the main package. Now I need to pass this variable to all other packages. How can this be done without explicit passing during a function call from another package? Thanks in advance.
Answer the question
In order to leave comments, you need to log in
To you at first the book to esteem, manuals. And then ask questions.
Here is a good transfer mechanism, with goodies and reservations, called environment-variables.
And access to the database can be treated as an environment variable
https://gobyexample.com/environment-variables
Use it to your health, but in moderation.
package conf
func init() {
//Здесь может быть, к примеру, погрузка данных из конфигурационных файлов
s:="hello global world";
os.Setenv("MyGlobalEnv", s);
}
package my_programm
func main() {
fmt.Println("global_var:", os.Getenv("MyGlobalEnv"))
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question