Skip to content

Package envconf implements reading config from enviroment variables.

License

Notifications You must be signed in to change notification settings

krhubert/envconf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Envconf

Build Status Coverage Status Go Report Card GitHub license GoDoc

Package envconf implements reading config from enviroment variables.

import "github.com/krhubert/envconfig"

Usage

package main

type Config struct {
    Host string `envconf:"name,default,required"`
}

Tag:

  • name - the name of env variable (if prefix is set, it will be added to name -> prefix_name)
  • default - if no env variable found, the default value will be used
  • required - can be set to true, then user must set this variable (required can't be combined with default)

Example

package main

import (
    "log"

    "github.com/krhubert/envconf"
)

type Config struct {
    Host    string `envconf:"host,,true"`
    Port    int    `envconf:"port,80"`
    Timeout int    `envconf:"timeout"`
}

Basic usage

func main() {
    var conf Config
    if err := envconf.SetValues(&conf); err != nil {
        log.Fatal(err)
    }
    log.Printf("host=%s, port=%d, timeout=%d\n", conf.Host, conf.Port, conf.Timeout)
}
$ HOST=127.0.0.1 PORT=8080 TIMEOUT=60 ./main
host=127.0.0.1, port=8080, timeout=60
$

Application prefix can be set by creating config

func main() {
    var conf Config
    if err := envconf.NewConfig("app").SetValues(&conf); err != nil {
        log.Fatal(err)
    }
    log.Printf("host=%s, port=%d, timeout=%d\n", conf.Host, conf.Port, conf.Timeout)
}
$ APP_HOST=127.0.0.1 APP_PORT=8080 APP_TIMEOUT=60 ./main
host=127.0.0.1, port=8080, timeout=60
$

About

Package envconf implements reading config from enviroment variables.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages