envcfg Build Status

Package envcfg provides environment variable mapping to structs.

It can be used to read configuration parameters from the environment.

Fields for which environment variables can be found are overwritten, otherwise they are left to their previous value.

This package can be used, for example, after gcfg to override settings provided in a configuration file.

import "github.com/Pacien/envcfg"


Package documentation can be found on GoDoc

Usage example

Set environment variables:

export PORT=8080

Create a struct optionally with tagged fields and/or already set values, then call the ReadInto function to read the values set in the environment variables.

package main

import (

type Config struct {
    Server struct {
        Port        int `env:"PORT" absenv:"true"`
    User struct {
        Username    string
        Password    string

var cnf Config

func (c *Config) setDefaults() *Config {
    c.User.Username = "root"
    c.User.Password = "password"
    return c

func init() {

    _, errs := envcfg.ReadInto(&cnf)
    if len(errs) != 0 {

func main() {

Output of the previous program:

{{8080} {root S3cUrE}}

See tests for other examples.


This program is published under the MIT License. See the LICENSE.txt file.