A
A
Adatan2019-12-26 22:32:59
go
Adatan, 2019-12-26 22:32:59

The problem of global variables?

Question for people who know the subtleties of the Go language. What is the problem of using global variables in a project? For example, in some cases I use a global database variable so that in the functions I need (in different packages) I can perform any actions with it. Are there any other ways to interact data (variables) between packages?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2019-12-26
@ghostiam

Pass a structure with methods/database interface explicitly to another function/package
In order not to repeat yourself, see my other answer about the logger, the principle is the same
https://qna.habr.com/answer?answer_id=1427366#answ...
Problems:
If If you decide to move the code to another project or cover it with tests, then it will be very difficult to do this with global variables.
Also, for now you have one global variable with the database, and later there will be more and more, which will complicate code support, since it will not be clear where this variable comes from, who creates it, in what order you need to execute the code so that the global variable was not empty, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question