I have a Go main-application that uses a custom log
package and a custom config
package.
This application "consumes" other packages that they need more logging or config parameters. Perhaps, a useful example is a custom api-package
:
- It should print some
debug|info|warning
logs (the errors are managed as usual by the caller, in this case, the main-application). - It should have access to values stored in the config (e.g. a server url/port, the environment of the main-application (development, test or production),... ).
I see three solutions to solve this problem:
- Passing a pointer (of
log
andconfig
) from the main object to the sub-package when the package is "initialised", then it will use this pointer like the main-application. - Initialise these dependencies (
log
orconfig
) directly into the sub-package too, but this could raise concurrency issues between the main-application's instance vs. the package's instance. - Pass to each method the required arguments (but I would like to avoid build methods with huge args list).
At the moment I use method 1, am I doing it right? What is the Go conventions about this? Are there other ways to solve this?
EDIT
Just to explain, why it is not a duplicate of the proposed "duplicated" questions:
- The answer proposed by icza: it takes care only for
logging
(writing "somewhere") and not forconfig
(reading and writing). No other options are taken in consideration, for example which one follow at best the standards, which one is more performant,... - The other answer proposed by icza: it takes only care about a single application, this is not the case at all, because I already know how use the
logging
and theconfig
in one "application" (or in goroutines).
If you mark as duplicate, please just explain why. So I can see if this is a really duplicate or if my question was misunderstand.