Awesome Open Source
Awesome Open Source


Build Status

Envy makes working with ENV variables in Go trivial.

  • Get ENV variables with default values.
  • Set ENV variables safely without affecting the underlying system.
  • Temporarily change ENV vars; useful for testing.
  • Map all of the key/values in the ENV.
  • Loads .env files (by using godotenv)
  • More!


$ go get -u


func Test_Get(t *testing.T) {
	r := require.New(t)
	r.Equal(os.Getenv("GOPATH"), envy.Get("GOPATH", "foo"))
	r.Equal("bar", envy.Get("IDONTEXIST", "bar"))

func Test_MustGet(t *testing.T) {
	r := require.New(t)
	v, err := envy.MustGet("GOPATH")
	r.Equal(os.Getenv("GOPATH"), v)

	_, err = envy.MustGet("IDONTEXIST")

func Test_Set(t *testing.T) {
	r := require.New(t)
	_, err := envy.MustGet("FOO")

	envy.Set("FOO", "foo")
	r.Equal("foo", envy.Get("FOO", "bar"))

func Test_Temp(t *testing.T) {
	r := require.New(t)

	_, err := envy.MustGet("BAR")

	envy.Temp(func() {
		envy.Set("BAR", "foo")
		r.Equal("foo", envy.Get("BAR", "bar"))
		_, err = envy.MustGet("BAR")

	_, err = envy.MustGet("BAR")

.env files support

Envy now supports loading .env files by using the godotenv library. That means one can use and define multiple .env files which will be loaded on-demand. By default, no env files will be loaded. To load one or more, you need to call the envy.Load function in one of the following ways:

envy.Load() // 1

envy.Load("MY_ENV_FILE") // 2

envy.Load(".env", "") // 3

envy.Load(".env", "NON_EXISTING_FILE") // 4

// 5

// 6
envy.Load(".env", "NON_EXISTING_FILE", "")
  1. Will load the default .env file
  2. Will load the file MY_ENV_FILE, but not .env
  3. Will load the file .env, and after that will load the file. If any variable is redefined in . it will be overwritten (will contain the value)
  4. Will load the .env file and return an error as the second file does not exist. The values in .env will be loaded and available.
  5. Same as 4
  6. Will load the .env file and return an error as the second file does not exist. The values in .env will be loaded and available, but the ones in won't.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
go (14,423
golang (3,675
environment (63
helpers (29

Find Open Source By Browsing 7,000 Topics Across 59 Categories