go get github.com/chyroc/go-loaderThe go-loader software is divided into two parts: data extract layer: Extractor and data transform layer: Transformer.
Use the tag of the structure in go to define what Extractor and Transformer are used by the field, and what their parameters are.
go-loader has several built-in data Extractor and Transformer.
- build-in
Extractorenvfileinherit
- build-in
Transformer
Of course, users can also customize:
WithExtractor(extractor1, extractor2)
WithTransformer(transformer1, transformer2)The structure field uses loader as the tag name to accept parameters from the user. such as:
type Conf struct {
GitHubToken string `loader:"env,GITHUB_TOKEN"`
}In tag loader, read several strings separated by , in turn, the first of which is the name of the Extractor,
and the subsequent list of strings are the parameters of the Extractor function execution
In the above example, the Extractor is env, and GITHUB_TOKEN is passed as a parameter to the env Extractor for processing
You can define only Extractor without defining Transformer. The loader:"env,GITHUB_TOKEN" listed in the previous example only has Extractor
If you still need to define Transformer, separate it with ;
and then a list of strings separated by , where the first string is the name of Transformer, and the following string list is the parameters of Transformer.
type Conf struct {
GitHubToken string `loader:"env,JSON_TOKEN;json,.GITHUB"`
}In this example, env JSON_TOKEN stores the json data of a token, then after the Extractor extract the data,
it also needs to use the json Transformer to convert the final data from the GITHUB key
- load_env: load data from env
