gotenv

package module
v1.1.0 Latest Latest
Warning

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

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

README

gotenv

Build Status Coverage Status Go Report Card GoDoc

Load environment variables dynamically in Go.

Installation

$ go get github.com/subosito/gotenv

Usage

Store your configuration to .env file on your root directory of your project:

APP_ID=1234567
APP_SECRET=abcdef

You may also add export in front of each line so you can source the file in bash:

export APP_ID=1234567
export APP_SECRET=abcdef

Put the gotenv package on your import statement:

import "github.com/subosito/gotenv"

Then somewhere on your application code, put:

gotenv.Load()

Behind the scene it will then load .env file and export the valid variables to the environment variables. Make sure you call the method as soon as possible to ensure all variables are loaded, say, put it on init() function.

Once loaded you can use os.Getenv() to get the value of the variable.

Here's the final example:

package main

import (
	"github.com/subosito/gotenv"
	"log"
	"os"
)

func init() {
	gotenv.Load()
}

func main() {
	log.Println(os.Getenv("APP_ID"))     // "1234567"
	log.Println(os.Getenv("APP_SECRET")) // "abcdef"
}

You can also load other than .env file if you wish. Just supply filenames when calling Load():

gotenv.Load(".env.production", "credentials")

That's it :)

Another Scenario

Just in case you want to parse environment variables from any io.Reader, gotenv keeps its Parse() function as public API so you can utilize that.

// import "strings"

pairs := gotenv.Parse(strings.NewReader("FOO=test\nBAR=$FOO"))
// gotenv.Env{"FOO": "test", "BAR": "test"}

pairs = gotenv.Parse(strings.NewReader(`FOO="bar"`))
// gotenv.Env{"FOO": "bar"}

Parse ignores invalid lines and returns Env of valid environment variables.

Notes

The gotenv package is a Go port of dotenv project. Most logic and regexp pattern is taken from there and aims will be compatible as close as possible.

Documentation

Overview

Package gotenv provides functionality to dynamically load the environment variables

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply added in v1.0.0

func Apply(r io.Reader) error

Apply is function to load an io Reader then export the valid variables into environment variables if they are not exist.

func Load

func Load(filenames ...string) error

Load is function to load a file or multiple files and then export the valid variables into environment variables if they are not exists. When it's called with no argument, it will load `.env` file on the current path and set the environment variables. Otherwise, it will loop over the filenames parameter and set the proper environment variables.

func MustLoad added in v1.0.0

func MustLoad(filenames ...string)

MustLoad is similar function like Load but will panic when supplied files are not exist.

func MustOverLoad added in v1.0.0

func MustOverLoad(filenames ...string)

MustOverLoad is similar function like OverLoad but will panic when supplied files are not exist.

func OverApply added in v1.0.0

func OverApply(r io.Reader) error

OverApply is function to load an io Reader then export and override the valid variables into environment variables.

func OverLoad added in v1.0.0

func OverLoad(filenames ...string) error

OverLoad is function to load a file or multiple files and then export and override the valid variables into environment variables.

Types

type Env

type Env map[string]string

Env holds key/value pair of valid environment variable

func Parse

func Parse(r io.Reader) Env

Parse is a function to parse line by line any io.Reader supplied and returns the valid Env key/value pair of valid variables. It expands the value of a variable from environment variable, but does not set the value to the environment itself. This function is skipping any invalid lines and only processing the valid one.

Example
package main

import (
	"fmt"
	"github.com/subosito/gotenv"
	"strings"
)

func main() {
	pairs := gotenv.Parse(strings.NewReader("FOO=test\nBAR=$FOO"))
	fmt.Printf("%+v\n", pairs) // gotenv.Env{"FOO": "test", "BAR": "test"}

	pairs = gotenv.Parse(strings.NewReader(`FOO="bar"`))
	fmt.Printf("%+v\n", pairs) // gotenv.Env{"FOO": "bar"}
}
Output:

func StrictParse added in v1.0.0

func StrictParse(r io.Reader) (Env, error)

StrictParse is a function to parse line by line any io.Reader supplied and returns the valid Env key/value pair of valid variables. It expands the value of a variable from environment variable, but does not set the value to the environment itself. This function is returning an error if there is any invalid lines.

type ErrFormat added in v1.1.0

type ErrFormat struct {
	Message string
}

ErrFormat is an error for invalid line format

func (ErrFormat) Error added in v1.1.0

func (e ErrFormat) Error() string

Jump to

Keyboard shortcuts

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