envcfg: github.com/tomazk/envcfg Index | Files | Directories

package envcfg

import "github.com/tomazk/envcfg"

Un-marshaling environment variables to Go structs

Getting Started

Let's set a bunch of environment variables and then run your go app

#!/usr/bin/env bash
export DEBUG="false"
export DB_HOST="localhost"
export DB_PORT="8012"

./your_go_app

Within your Go app do

import "github.com/tomazk/envcfg"

// declare a type that will hold your env variables
type Cfg struct {
	DEBUG bool
	DB_PORT int
	DB_HOST string
}

func main() {
	var config Cfg
	envcfg.Unmarshal(&config)
	// config is now set to Config{DEBUG: false, DB_PORT: 8012, DB_HOST: "localhost"}

	// optional: clear env variables listed in the Cfg struct
	envcfg.ClearEnvVars(&config)

}

More documentation in README: https://github.com/tomazk/envcfg

Index

Package Files

envcfg.go

func ClearEnvVars Uses

func ClearEnvVars(v interface{}) error

ClearEnvVars will clear all environment variables based on the struct field names or struct field tags. It will keep all those with envcfgkeep:"" struct field tag. It will return an error, if it recieves an unsupported non-struct type, if types of the fields are not supported

func Unmarshal Uses

func Unmarshal(v interface{}) error

Unmarshal will read your environment variables and try to unmarshal them to the passed struct. It will return an error, if it recieves an unsupported non-struct type, if types of the fields are not supported or if it can't parse value from an environment variable, thus taking care of validation of environment variables values.

Directories

PathSynopsis
_example

Package envcfg imports 8 packages (graph) and is imported by 2 packages. Updated 2018-03-08. Refresh now. Tools for package owners.