L
L
levisl2016-03-23 12:58:55
go
levisl, 2016-03-23 12:58:55

Should I use global variables for configs in golang?

What is the correct way to write the config structure to a global variable and pull it from all functions, or write it locally and pass the pointer to each function that uses parameters from it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita, 2016-03-23
@bitver

A global (or rather public in a separate package for convenience) variable is used very often, but this is in cases where you will not programmatically change the value of the variable. If the values ​​in the variable will change programmatically, then you will have to add a module so that there is not only 1 variable (already closed), but a couple of public methods with mutexes or working with channels - as you wish.

O
Oleg Shevelev, 2016-03-23
@mantyr

Here are the questions that arise when writing an engine for configs:
Also, do not forget that there are network services for configs with the same features, but with their own specifics.
I have not yet seen the ideal config for Golang, but there are very interesting ones.
There is also a question - what will the engine do if your program expects some data in the config, and there is an error in the file itself or suppose there are no fields .
Some configs working on the principle of "take a file and throw it on the structure" in this case do not work because the structure does not match. And it may not coincide very often, because clients and their employees do not know in advance that this is a very sensitive place in the program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question