env

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2018 License: MIT Imports: 3 Imported by: 421

README

What is it?

Build Status

The env package is to simplify reading environment variables with e.g. the flag package.

Example:

In our main, we often allow users to initialize our programs via flags. When users leave out the flags, we want to fall back to environment variables. The env package simplifies this scenario.

import (
    "flag"
    "fmt"
)

func main() {
	var (
		// Parse addr from flag, use HTTP_ADDR and ADDR env vars as fallback
		addr = flag.String("addr", env.String("127.0.0.1:3000", "HTTP_ADDR", "ADDR"), "Bind to this address")
	)
	flag.Parse()

	fmt.Println(*addr)
	// Output: 127.0.0.1:3000
}

License

MIT. See LICENSE file.

Documentation

Overview

Package env simplifies reading values from environment variables.

It is typically used in the main function to initialize variables from the flag package, then fallback to using environment variables.

Example
package main

import (
	"flag"
	"fmt"

	"github.com/olivere/env"
)

func main() {
	var (
		// Parse addr from flag, use HTTP_ADDR and ADDR env vars as fallback
		addr = flag.String("addr", env.String("127.0.0.1:3000", "HTTP_ADDR", "ADDR"), "Bind to this address")
	)
	flag.Parse()

	fmt.Println(*addr)
}
Output:

127.0.0.1:3000

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(defaultValue bool, envvars ...string) bool

Bool inspects the environment variables specified in envvars. If all of these environment variables are empty, it returns defaultValue.

func Duration

func Duration(defaultValue time.Duration, envvars ...string) time.Duration

Duration inspects the environment variables specified in envvars. If all of these environment variables are empty, it returns defaultValue.

func Float32

func Float32(defaultValue float32, envvars ...string) float32

Float32 inspects the environment variables specified in envvars. If all of these environment variables are empty, it returns defaultValue.

func Float64

func Float64(defaultValue float64, envvars ...string) float64

Float64 inspects the environment variables specified in envvars. If all of these environment variables are empty, it returns defaultValue.

func Int

func Int(defaultValue int, envvars ...string) int

Int inspects the environment variables specified in envvars. If all of these environment variables are empty, it returns defaultValue.

func Int64

func Int64(defaultValue int64, envvars ...string) int64

Int64 inspects the environment variables specified in envvars. If all of these environment variables are empty, it returns defaultValue.

func String

func String(defaultValue string, envvars ...string) string

String inspects the environment variables specified in envvars. If all of these environment variables are empty, it returns defaultValue.

func Time

func Time(defaultValue time.Time, layout string, envvars ...string) time.Time

Time inspects the environment variables specified in envvars. If all of these environment variables are empty, it returns defaultValue.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL