Skip to content
/ env Public

Is a simple environment variable parser inspired by the go flag package.

License

Notifications You must be signed in to change notification settings

jksch/env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Env

Build Status Coverage Status Go Report Card GoDoc License

Is a simple environment variable parser inspired by the go flag package.

Env allows you to simply parse an environment variable or specify a default.

Basic usage

Assume this environmental variables are set:

DB_USER=admin
DB_PASS=password

You can do the following:

package main

import (
	"fmt"
	"time"

	"github.com/jksch/env"
)

var dbHost = env.String("DB_HOST", "localhost")
var dbPort = env.Int("DB_PORT", 27017)
var dbUser = env.String("DB_USER", "")
var dbPass = env.String("DB_PASS", "")
var dbTimeout = env.Duration("DB_TIMEOUT", 15*time.Second)

func main() {
	if err := env.ParserError(); err != nil {
		fmt.Printf("Could not parse env variables, %v", err)
	}
	fmt.Printf("dbHost: %v\n", dbHost)
	fmt.Printf("dbPort: %v\n", dbPort)
	fmt.Printf("dbUser: %v\n", dbUser)
	fmt.Printf("dbPass: %v\n", dbPass)
	fmt.Printf("dbTimeout: %v\n", dbTimeout)
}

This will parse two environmental variables and load three defaults:

dbHost: localhost
dbPort: 27017
dbUser: admin
dbPass: password
dbTimeout: 15s

About

Is a simple environment variable parser inspired by the go flag package.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages