Giter VIP home page Giter VIP logo

gotenv's Introduction

gotenv

Build Status Coverage Status Go Report Card GoDoc

Load environment variables dynamically in Go.

Installation

$ go get github.com/subosito/gotenv

Usage

Store your configuration to .env file on your root directory of your project:

APP_ID=1234567
APP_SECRET=abcdef

You may also add export in front of each line so you can source the file in bash:

export APP_ID=1234567
export APP_SECRET=abcdef

Put the gotenv package on your import statement:

import "github.com/subosito/gotenv"

Then somewhere on your application code, put:

gotenv.Load()

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

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

Here's the final example:

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():

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

That's it :)

Another Scenario

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

// import "strings"

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

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

Parse ignores invalid lines and returns Env of valid environment variables.

Notes

The gotenv package is a Go port of dotenv project. Most logic and regexp pattern is taken from there and aims will be compatible as close as possible.

gotenv's People

Contributors

subosito avatar atomaths avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.