All Projects → subosito → Gotenv

subosito / Gotenv

Licence: mit
Load environment variables from `.env` or `io.Reader` in Go.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Gotenv

Envinject Plugin
This plugin makes it possible to setup a custom environment for your jobs
Stars: ✭ 74 (-61.66%)
Mutual labels:  environment-variables
Config
A lightweight yet powerful config package for Go projects
Stars: ✭ 126 (-34.72%)
Mutual labels:  environment-variables
Envy
Envy automatically exposes environment variables for all of your Go flags
Stars: ✭ 150 (-22.28%)
Mutual labels:  environment-variables
React Env
Runtime environment variables for react apps.
Stars: ✭ 90 (-53.37%)
Mutual labels:  environment-variables
Cra Runtime Environment Variables
Guide: Runtime environment variables within Create React App
Stars: ✭ 111 (-42.49%)
Mutual labels:  environment-variables
Python Decouple
Strict separation of config from code.
Stars: ✭ 1,982 (+926.94%)
Mutual labels:  environment-variables
Env Providers
👷 Load Laravel service providers based on your application's environment.
Stars: ✭ 73 (-62.18%)
Mutual labels:  environment-variables
Envdecode
Go package for populating structs from environment variables using struct tags
Stars: ✭ 174 (-9.84%)
Mutual labels:  environment-variables
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+978.76%)
Mutual labels:  environment-variables
Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+5935.23%)
Mutual labels:  environment-variables
Flex Env
🌳 Manage your .env file in Laravel projects through artisan
Stars: ✭ 95 (-50.78%)
Mutual labels:  environment-variables
Fsconfig
FsConfig is a F# library for reading configuration data from environment variables and AppSettings with type safety.
Stars: ✭ 108 (-44.04%)
Mutual labels:  environment-variables
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+861.14%)
Mutual labels:  environment-variables
Phoenix gon
🔥 Phoenix variables in your JavaScript without headache.
Stars: ✭ 84 (-56.48%)
Mutual labels:  environment-variables
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+1021.24%)
Mutual labels:  environment-variables
Env
Simple library to read environment variables and convert to simple types.
Stars: ✭ 73 (-62.18%)
Mutual labels:  environment-variables
Envy
😠 Environmentally friendly environment variables
Stars: ✭ 132 (-31.61%)
Mutual labels:  environment-variables
Aconfig
Simple, useful and opinionated config loader.
Stars: ✭ 187 (-3.11%)
Mutual labels:  environment-variables
Nuxt Env
Inject env vars for your Nuxt app at runtime
Stars: ✭ 169 (-12.44%)
Mutual labels:  environment-variables
Fig
A minimalist Go configuration library
Stars: ✭ 142 (-26.42%)
Mutual labels:  environment-variables

gotenv

Build Status Coverage Status Go Report Card GoDoc

Load environment variables from .env or io.Reader in Go.

Usage

Put the gotenv package on your import statement:

import "github.com/subosito/gotenv"

To modify your app environment variables, gotenv expose 2 main functions:

  • gotenv.Load
  • gotenv.Apply

By default, gotenv.Load will look for a file called .env in the current working directory.

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 it loads all variables, say, put it on init() function.

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

Let's say you have .env file:

APP_ID=1234567
APP_SECRET=abcdef

Here's the example of your app:

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(). It will load them in order and the first value set for a variable will win.:

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

While gotenv.Load loads entries from .env file, gotenv.Apply allows you to use any io.Reader:

gotenv.Apply(strings.NewReader("APP_ID=1234567"))

log.Println(os.Getenv("APP_ID"))
// Output: "1234567"

Both gotenv.Load and gotenv.Apply DO NOT overrides existing environment variables. If you want to override existing ones, you can see section below.

Environment Overrides

Besides above functions, gotenv also provides another functions that overrides existing:

  • gotenv.OverLoad
  • gotenv.OverApply

Here's the example of this overrides behavior:

os.Setenv("HELLO", "world")

// NOTE: using Apply existing value will be reserved
gotenv.Apply(strings.NewReader("HELLO=universe"))
fmt.Println(os.Getenv("HELLO"))
// Output: "world"

// NOTE: using OverApply existing value will be overridden
gotenv.OverApply(strings.NewReader("HELLO=universe"))
fmt.Println(os.Getenv("HELLO"))
// Output: "universe"

Throw a Panic

Both gotenv.Load and gotenv.OverLoad returns an error on something wrong occurred, like your env file is not exist, and so on. To make it easier to use, gotenv also provides gotenv.Must helper, to let it panic when an error returned.

err := gotenv.Load(".env-is-not-exist")
fmt.Println("error", err)
// error: open .env-is-not-exist: no such file or directory

gotenv.Must(gotenv.Load, ".env-is-not-exist")
// it will throw a panic
// panic: open .env-is-not-exist: no such file or directory

Another Scenario

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

// import "strings"

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

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

Parse ignores invalid lines and returns Env of valid environment variables, while StrictParse returns an error for invalid lines.

Notes

The gotenv package is a Go port of dotenv project with some additions made for Go. For general features, it aims to be compatible as close as possible.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].