gotenv – github.com/subosito/gotenv Index | Examples | Files

package gotenv

import "github.com/subosito/gotenv"

Package gotenv provides functionality to dynamically load the environment variables

Index

Examples

Functions

func Apply

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

func MustLoad(filenames ...string)

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

func MustOverLoad

func MustOverLoad(filenames ...string)

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

func OverApply

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

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

Code:play 

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"}
}

func StrictParse

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

type ErrFormat struct {
	Message string
}

ErrFormat is an error for invalid line format

func (ErrFormat) Error

func (e ErrFormat) Error() string

Source Files

gotenv.go

Version
v1.1.0
Published
Jan 20, 2017
Platform
darwin/amd64
Imports
6 packages
Last checked
now

Tools for package owners.