V
V
v- death2015-08-17 14:55:58
go
v- death, 2015-08-17 14:55:58

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

4 answer(s)
A
Andrey K, 2015-08-17
@vGrabko99

No way.

A
Artem, 2015-08-17
@artem_kovardin

To you at first the book to esteem, manuals. And then ask questions.

V
Vitaly Filinkov, 2015-08-17
@vitfil

For this you need to put in a corner!

V
voltag, 2018-10-11
@voltag

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"))
}

and do not pay attention to the trolls with amazing "angles", "manuals", "bullets" and "nothing" they also need to somehow maintain their CSF

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question