• Stars
    star
    282
  • Rank 146,549 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

More Repositories

1

flutter-action

Flutter environment for use in GitHub Actions. It works on Linux, Windows, and macOS.
Shell
2,163
star
2

gingerice

Ruby wrapper for correcting spelling and grammar mistakes based on the context of complete sentences.
Ruby
481
star
3

iglo

API blueprint's formatter
Go
332
star
4

shorturl

Generic implementation for interacting with various URL shortening services in Go.
Go
85
star
5

twilio

Simple Twilio API wrapper in Go
Go
72
star
6

mascherano

Capistrano 3.x recipes
Ruby
39
star
7

audiojs-rails

AudioJS on rails asset pipeline
Ruby
23
star
8

openshift-ruby

Redhat's Openshift runs Ruby 2.0
Ruby
20
star
9

sixpack-go

Go client library for SeatGeek's Sixpack AB testing framework.
Go
19
star
10

gozaru

Filename sanitization for Go
Go
5
star
11

confreaks

confreaks on the command line
Go
5
star
12

simple_gravatar

Gravatar library for Dart 2
Dart
5
star
13

bootstrap-sass-rtl-rails

RTL version of bootstrap-sass
Ruby
4
star
14

gpaste-menu

dmenu + gpaste-client
Shell
4
star
15

tiffany

Go vanity URL
Go
3
star
16

plur

Opinionated development workflow and commonly used tools for Ruby on Rails based development.
Ruby
3
star
17

rails-openshift-action-hooks

Custom action hooks for rails deployment on openshift DIY
Shell
1
star
18

subosito.github.io

~
1
star
19

vscd

Visual Studio Code customization tool
JavaScript
1
star
20

gsapi

go-search.org api
Go
1
star
21

image64

Convert an image to base64 data URI scheme
Go
1
star
22

shorticon

A simple package for fetching a website's favicon.
Go
1
star
23

dropbox-light-icons

Dropbox status icons for dark panel.
Shell
1
star
24

flutter-action-example

Test cases for flutter-action
1
star