Package config
parses 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 parsing 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 parse config: %s", err)
}
fmt.Println(cfg.Username)
fmt.Println(cfg.Tag)
}
go get github.com/andreiavrammsd/config
See Makefile and VS Code setup.
- In some cases, quoted values are not parsed correctly.
MULTILINE_QUOTED="this is a multiline
quoted
value"
QUOTED_INCLUDING_QUOTES="{ \"name\": \"John\", \"age\": 30 }"