env

package module
v0.0.0-...-1b83f6d Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2017 License: MIT Imports: 6 Imported by: 0

README

go-env

Build Status Go Report Card GoDoc

go-env is a go library for using environment variables to configure your project.

Usage:
    // Field appears in env as key "FEILD" and
    // the field is omitted from the object if its value is empty,
    // as defined
    Field int `env:"FEILD"`
    // Field appears in env as key "FEILD" and
    // the field is omitted 1 if its value is empty.
    Field int `env:"FEILD,1"`
    // Field is ignored by this package.
    Field int `env:"-"`
    // The upper key name will be used if it's a non-empty string consisting of
    // only Unicode letters, digits, dollar signs, percent signs, hyphens, underscores and slashes.
    Field int `env:",1"`

Example

// These examples demonstrate more intricate uses of the env package.
package main

import (
    "log"
    "os"

    "github.com/sakeven/go-env"
)

func Assert(expression bool) {
    if !expression {
        log.Fatal("Error expression false")
    }
}

func ParseStruct() {
    type Inner struct {
        Num  int    `env:"NUMBER"`
        Str  string `env:"HELLO"`
        Bool bool   `env:"YES"`
    }

    type Test struct {
        Num     int    `env:"NUMBER"`
        Str     string `env:"HELLO"`
        Bool    bool   `env:"YES"`
        Default int    `env:"DEFAULT,12"`
        Skip    string `env:"-"`
        Inner   *Inner
    }

    os.Setenv("HELLO", "world")
    os.Setenv("NUMBER", "1")
    os.Setenv("YES", "true")
    os.Setenv("SKIP", "skip")

    os.Setenv("INNER_HELLO", "inner world")
    os.Setenv("INNER_NUMBER", "3")
    os.Setenv("INNER_YES", "true")

    test := new(Test)
    err := env.Decode(test)
    if err != nil {
        log.Fatal(err)
    }

    Assert(test.Bool == true)
    Assert(test.Str == "world")
    Assert(test.Num == 1)
    Assert(test.Default == 12)
    Assert(test.Skip == "")
    Assert(test.Inner != nil)
    Assert(test.Inner.Num == 3)
    Assert(test.Inner.Bool == true)
    Assert(test.Inner.Str == "inner world")
}

func LoadEnvSet() {
    os.Setenv("NUMBER", "1")
    envSet := env.Load()

    n := envSet.Int("NUMBER", 2)

    Assert(n == 1)
}

func main() {
    ParseStruct()
    LoadEnvSet()
    log.Println("Parse env success")
}

Documentation

Overview

Package env is a go library for using environment variable to configure your project Usage:

// Field appears in env as key "FEILD" and
// the field is omitted from the object if its value is empty,
// as defined
Field int `env:"FEILD"`
// Field appears in env as key "FEILD" and
// the field is omitted 1 if its value is empty.
Field int `env:"FEILD,1"`
// Field is ignored by this package.
Field int `env:"-"`
// The upper key name will be used if it's a non-empty string consisting of
// only Unicode letters, digits, dollar signs, percent signs, hyphens, underscores and slashes.
Field int `env:",1"`

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(i interface{}) error

Decode parse the os environment variables and stores the result in the value pointed to by i. the value pointed to by i must be string, int, bool, or struct based on string, int, bool.

Types

type Set

type Set map[string]string

Set stores all environment variables <key, valuer> pair.

func LoadSet

func LoadSet() Set

LoadSet load environment variables to parse a new EnvSet.

func (Set) Bool

func (e Set) Bool(key string, defaultValue bool) bool

Bool bind bool value of specific key.

func (Set) Int

func (e Set) Int(key string, defaultValue int) int

Int bind int value of specific key.

func (Set) Int64

func (e Set) Int64(key string, defaultValue int64) int64

Int64 bind int value of specific key.

func (*Set) Reload

func (e *Set) Reload()

Reload refresh envSet from environment.

func (Set) String

func (e Set) String(key string, defaultValue string) string

String bind string value of specific key.

Directories

Path Synopsis
These examples demonstrate more intricate uses of the env package.
These examples demonstrate more intricate uses of the env package.

Jump to

Keyboard shortcuts

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