Skip to content
forked from joho/godotenv

A fork of godotenv with focus not only on dotenv, but on system variables too.

License

Notifications You must be signed in to change notification settings

alois9866/godotenv

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDotEnv CI Go Report Card

This is a fork of godotenv. It rethinks some points of the original idea, narrowing the possible use scenarios. The main purpose of the change is to be able to read not only dotenv variables, but also system variables.

This project contains only the functions that I consider to be essential. If you need a more feature-rich solution, please check out the original repository.

Installation

go get github.com/alois9866/godotenv

Usage

Add your application configuration to your .env file in the root of your project:

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

You can also set some variables in your shell or when running you app as ANOTHER_KEY=KEYGOESHERE ./app

Then in your Go app you can do something like

package main

import (
    "github.com/alois9866/godotenv"
)

func main() {
    env, _ := godotenv.Get()

    s3Bucket := env["S3_BUCKET"]
    secretKey := env["SECRET_KEY"]
    anotherKey := env["ANOTHER_KEY"]

    // ...
}

Basically if you want to read all environment variables, you can call:

env, _ := godotenv.Get()

By default, dotenv variables will take precedence over system variables. If you want to use values from system environment over values from dotenv files, you can use this:

env, _ := godotenv.Get(PrioritizeSystem())

If you want to check that some specific variables are available, you can call:

env, notFound := godotenv.Get(Variables("ENV_VAR1", "ENV_VAR2"))

In this case, if some of those variables are not set, notFound will contain their names.

If you want to use files other than .env, you can do that too:

env, notFound := godotenv.Get(Variables("ENV_VAR1", "ENV_VAR2"), From("file1", "file2"))

File formatting

If you want to be really fancy with your env file you can do comments and exports (below is a valid env file):

# I am a comment and that is OK
SOME_VAR=someval
SOME_VAR2="someval2"
FOO=BAR # comments at line end are OK too
export BAR=BAZ

Or finally you can do YAML(ish) style:

FOO: bar
BAR: baz

If you want to know more about original dotenv usage convention, you can read about it here.

Who?

The original Ruby library dotenv was written by Brandon Keepers.

The original Go library was written by John Barton based off the tests/fixtures in the original library.

This version is written by me.

About

A fork of godotenv with focus not only on dotenv, but on system variables too.

Resources

License

Stars

Watchers

Forks

Languages

  • Go 97.8%
  • Shell 2.2%