Documentation ¶
Overview ¶
Package dotenv is an implementation of the Ruby dotenv library. The purpose of the library is to load variables from a file into the environment.
Index ¶
- Variables
- func Load(paths ...string) (err error)
- func LoadMap(envMap map[string]string, overload bool)
- func LoadReader(r io.Reader) error
- func Overload(paths ...string) (err error)
- func ParseString(s string) (key, value string, err error)
- func Read(rd io.Reader) (map[string]string, error)
- func ReadFile(path string) (map[string]string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidln = errors.New("invalid line") ErrEmptyln = errors.New("empty line") ErrCommentln = errors.New("comment line") )
Errors occurred during parsing.
Functions ¶
func Load ¶
Load will load a variadic number of environment config files. Will not overwrite currently set env vars.
Example ¶
package main import ( "fmt" "log" "os" "github.com/alexsasharegan/dotenv" ) func main() { err := dotenv.Load("fixtures/example.env") if err != nil { log.Fatal(err) } envKeys := []string{"S3_BUCKET", "SECRET_KEY", "MESSAGE"} for _, key := range envKeys { fmt.Printf("%s : %s\n", key, os.Getenv(key)) } }
Output: S3_BUCKET : YOURS3BUCKET SECRET_KEY : YOURSECRETKEYGOESHERE MESSAGE : A message containing important spaces.
func LoadReader ¶
LoadReader will load an environment config from a reader interface. Will not overwrite currently set env vars.
func Overload ¶
Overload will load a variadic number of environment config files. Overwrites currently set env vars.
func ParseString ¶
ParseString parses a given string into a key, value pair. Returns the key, value, and an error.
Example ¶
package main import ( "fmt" "github.com/alexsasharegan/dotenv" ) func main() { envStrs := []string{ `FOO=bar`, `FOO="escaped\"bar with quote"`, `FOO="bar\nbaz"`, `FOO.BAR=foobar`, `FOO="bar#baz" # comment`, `INVALID LINE`, } for _, s := range envStrs { k, v, err := dotenv.ParseString(s) if err != nil { fmt.Printf("parsing error: %v", err) continue } fmt.Printf("%s : %s\n", k, v) } }
Output: FOO : bar FOO : escaped"bar with quote FOO : bar baz FOO.BAR : foobar FOO : bar#baz parsing error: invalid line
func ReadFile ¶
ReadFile reads an env file at a given path, and return values as a map.
Example ¶
package main import ( "fmt" "log" "github.com/alexsasharegan/dotenv" ) func main() { env, err := dotenv.ReadFile("fixtures/example.env") if err != nil { log.Fatal(err) } fmt.Printf("%s : %s\n", "LIB", env["LIB"]) }
Output: LIB : github.com/alexsasharegan/dotenv
Types ¶
This section is empty.