Answer the question
In order to leave comments, you need to log in
How and where to initialize all components of a Go project in the OOP style?
Share how someone initializes components in a Go project?
For example, there is a repository package, jobs (where repository is used), service (where jobs are used), etc.
Answer the question
In order to leave comments, you need to log in
Recently I solved the same problem:
The first time I tried just hardcode, something like this :
+ it’s not a pity to rewrite completely
- poorly configurable
Second attempt - emphasis on configurability but also using pure go:
+ almost everything can be configured
- hard to maintain.
Third attempt:
Rewritten on the basis of a self-written service locator something like this :
+ little code, all settings in yaml
- we do not control the initialization process
Fourth attempt:
A structure for the configuration is created, filled with data via viper + launched via cobrasomething like this :
+ only the necessary is put into the settings, and not everything at all, as in the case of the service locator
+ the initialization process is completely controlled
In general, I can recommend the following:
1. create a structure for your entire application configuration;
2. write down user settings there at the beginning of initialization;
3. for all optional settings, there must be default values;
4. do not pursue maximum flexibility, this will lead you to a dead end;
5. Perform the creation of components directly on the basis of the generated configuration object, you should not pull out each setting separately through viper or its analogues.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question