Awesome Go

config

CategoryConfiguration
SubcategoryConfiguration
Stars0

Struct-based configuration loader with a dedicated config file parser, supporting env vars, flags, defaults, and validation

About config

Config

License build codecov GoDoc Go Report Card Socket.dev

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)
}

Install

go get github.com/andreiavrammsd/config

Usage

See examples and tests.

Testing and QA tools for development

See Makefile and VS Code setup.

Known issues

  • In some cases, quoted values are not parsed correctly.
MULTILINE_QUOTED="this is a multiline
quoted
value"

QUOTED_INCLUDING_QUOTES="{ \"name\": \"John\", \"age\": 30 }"

Frequently Asked Questions

What is config?

config is a Configuration library for the Go programming language. Struct-based configuration loader with a dedicated config file parser, supporting env vars, flags, defaults, and validation

How do I install config?

Install config with the Go module system using `go get andreiavrammsd/config`. Check the repository for the current installation instructions.

What category does config belong to?

config is listed under Configuration, specifically Configuration.

← Back to Configuration