Config

Package config
loads configuration values into given struct.
Requirements for configuration struct:
- A non-nil pointer to the struct must be passed.
- Fields must be exported. Unexported fields will be ignored.
- A field can have the
env
tag which defines the key of the value. If no tag provided, the key will be the uppercase full path of the field (all the fields names starting root until current field, joined by underscore).
- The
json
tag will be used for loading from JSON.
Input sources:
- environment variables
- environment variables from files
- byte array
- json
package main
import (
"fmt"
"log"
"github.com/andreiavrammsd/config"
)
type Config struct {
Username string `env:"CUSTOM_USERNAME_TAG"`
Tag string `default:"none"`
}
func main() {
input := []byte(`CUSTOM_USERNAME_TAG=msd # username`)
cfg := Config{}
if err := config.New().FromBytes(&cfg, input); err != nil {
log.Fatalf("cannot load config: %s", err)
}
fmt.Println(cfg.Username)
fmt.Println(cfg.Tag)
}
Install
go get github.com/andreiavrammsd/config
Usage
See examples and tests.
Testing and QA tools for development
See Makefile and VS Code setup.