Declarative configuration for Go 🚀

test-and-lint codecov

config is a package that supports reading configuration into a struct from files, environment variable and command line arguments. All you need is to declare a config structure and call Read method.

type Config struct {	
	DB                Database	
	Debug             Debug

type Database struct {
	Host       string
	Password   string
	DbName     string
	Username   string
	Port       int

func main() {
    var config Config
    err := config.NewConfReader("myconf").Read(&config)
    if err != nil {

If you want to change the DB Host of your applications the you can do any of the following:

  1. creating config file in json, yaml, toml.For example myconf.yaml

   host: "localhost"
  1. setting environment variables. Like DB_HOST=localhost
  2. setting command line arguments. Like --db_host=localhost

ConfReader merges values from all three sources in the following order:

  1. File
  2. Environment variables
  3. Command line arguments

ℹī¸ Refer to the example that illustrates how to use ConfReader.

Install :shipit:

go get  

How To Set Configuration Values 👷

Config File 📝


ConfReader will use config name property to search for a config file with that name.


By default, config reader will search for a config file in home or in current directory. You can override this behavior by using NewConfReader("myconf").WithSearchDirs("/etc/conf") of config builder

Referring fields

Field names are converted from camel case starting with lower case letter. For example if it code you refer to value as DB.DbName then it will be converted to

   dbName: "mydb"


Config file type could be any type supported by viper: JSON, TOML, YAML, HCL, INI, envfile and Java Properties files.

Environment Variables đŸ“Ļ

To set a flag via environment variable, make all letters uppercase and replace ‘.’ with ‘_’ in path. For example: app.Server.Port -> APP_SERVER_PORT

Environment variables are prefixed with config name by default. For example NewConfReader("myconf") will search for environment variables like MYCONF_DB_HOST This behavior could be overridden by setting NewConfReader("myconf").WithoutPrefix()

Command Line Arguments đŸ’ģ

To set a configuration field via command line argument you need to pass and argument prefixes wiht -- and lowercase field name with path. Like Boolean value could be set by passing only flag name like --verbose

You can override name for a flag by using tag flag:"name" on a field. For example:

type Config struct {		
	DebugMode             bool `flag:"debug"`

You can set the flag by calling myapp --debug

👮 Validations

You can validate fields of you configuration struct by using validate tag. For example:

type Config struct {		
    Host           string `validate:"required"`

If validation fails ConfReader.Read will return an error.

👏 Contributing

We love help! Contribute by forking the repo and opening a pull requests or by creating an issue.

⭐ Credits

This package is based Vipeer Special thanks to enviper for making environment variables work with viper.


View Github