env

package module
v0.0.0-...-e11b6e8 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2019 License: Apache-2.0 Imports: 11 Imported by: 6

README

env

Build Status GoDoc

Ease of Accessing Environment Varaibles in Golang

Installation
$ go get -v github.com/goanywhere/env
Usage
PORT=9394
SECRET_KEY=YOURSECRETKEY

You can double/single quote string values:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----HkVN9…-----END DSA PRIVATE KEY-----"

You can use export in front of each line just like your shell settings, so that you can source the file in your terminal directly:

export USERNAME=account@goanywhere.io
export PASSWORD=AccountPasswordGoesHere

Export these values, you can then access them using env now.

package main

import (
    "github.com/goanywhere/env"
)


func main() {
    env.Int("PORT")             // 9394
    env.String("SECRET_KEY")    // YOURSECERTKEY
    .......
}

env also supports custom struct for you to access the reflected values.

package main

import (
    "fmt"
    "github.com/goanywhere/env"
)

type Spec struct {
    App         string
    SecretKey   string  `env:"SECRET_KEY"`
}

func main() {
    var spec Spec

    env.Set("App", "myapplication")
    env.Set("SECRET_KEY", "wGv7ELIx8P8qsUit9OuWw2zwPEF0nXtvjIKZQOioAVuI5GnHSwBAeWZ6l4-SpIPT")
    env.Map(&spec)

    fmt.Printf("App: %s", spec.App)             // output: "App: myapplication"
    fmt.Printf("Secret: %s", spec.SecretKey)    // output: "Secret: wGv7ELIx8P8qsUit9OuWw2zwPEF0nXtvjIKZQOioAVuI5GnHSwBAeWZ6l4-SpIPT"
}

We also includes dotenv supports, simply add the application settings to file .env right under the root of your project:

test1  =  value1
test2 = 'value2'
test3 = "value3"
export test4=value4
package main

import (
    "fmt"

    "github.com/goanywhere/env"
)

func main() {
    // Load '.env' from current working directory.
    env.Load()

    fmt.Printf("<test1: %s>", env.String("test1"))  // output: "value1"
    fmt.Printf("<test2: %s>", env.String("test2"))  // output: "value2"
}
NOTES

Sensitive settings should ONLY be accessible on the machines that need access to them. NEVER commit them to a repository (even a private one) that is not needed by every development machine and server.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(key string, fallback ...bool) (value bool)

Bool retrieves boolean value from the environment.

func Float

func Float(key string, fallback ...float64) (value float64)

Float retrieves float (64) value from the environment.

func Get

func Get(key string) (value string, exists bool)

Get retrieves the string value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present.

func Int

func Int(key string, fallback ...int) (value int)

Int retrieves the integer values separated by comma from the environment.

func Int64

func Int64(key string, fallback ...int64) (value int64)

Int retrieves the 64-bit integer values separated by comma from the environment.

func Load

func Load(dotenv string)

Load parses & set the values in the given files into os environment.

func Map

func Map(spec interface{}) error

Map fetches the key/value pair from os.Environ into the given spec. Tags are supported via `env:ACTUAL_OS_KEY`.

func Set

func Set(key string, value interface{}) error

Set stores the value of the environment variable named by the key. It returns an error, if any.

func String

func String(key string, fallback ...string) (value string)

String retrieves the string value from environment named by the key.

func Strings

func Strings(key string, fallback ...[]string) (value []string)

Strings retrieves the string values separated by comma from the environment.

func Uint

func Uint(key string, fallback ...uint) (value uint)

Uint retrieves the unsigned integer values separated by comma from the environment.

func Uint64

func Uint64(key string, fallback ...uint64) (value uint64)

Uint64 retrieves the 64-bit unsigned integer values separated by comma from the environment.

Types

This section is empty.

Jump to

Keyboard shortcuts

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