Coder Social home page Coder Social logo

go-dotenv's Introduction

go-dotenv

A dotenv package for the Go programming language.

This module provides functions to load variables form a file into the environment. It is heavily influcenced by the original Ruby implementation (https://github.com/bkeepers/dotenv) and various other Go libraries, e.g. https://github.com/joho/godotenv.

Other than the most existing libraries this module provides a well defined interface giving the ability to implement further interpreters (so called 'Shells') to support individual syntax. A term 'Shell' was chosen because the overall goal of this interface was to have the ability to support various shell syntax, e.g. bash, zsh, fish, PowerShell, etc.

However, the interface is heavily extensible so that you can implement a 'Shell' with your own syntax or to support other file formats, e.g. TOML or JSON.

The current package implements only a Bash shell (making use of the 'echo' command of a real Bash) and a so called Basic shell, which implements a subset of the Bash functionality. This implementation is also cross platform.

Installation

To use this module you can simply download it in your Go environment with the following command.

go get github.com/dateiexplorer/go-dotenv

Usage

After you installed the module you can simply add e.g. a configuration file .env in the root of your project with the following lines:

SECRET_KEY=YOURSECRETKEY
I_AM_COOL=$USER is cool!

Then in your Go application you can do something like this:

package main

import (
    "log"
    "os"

    "github.com/dateiexplorer/go-dotenv"
)

func main() {
    err := dotenv.Load()
    if err != nil {
        log.Fatal("Error loading .env file")
    }

    secretKey := os.Getenv("SECRET_KEY")
    msg := os.Getenv("I_AM_COOL")

    // Now you can whatever you suppose to do with that vairables
    log.Println(msg)
    log.Printf("Never log your secret key: %v\n", secretKey)
}

You can also specify the name of your env file as well as the Shell you want to use for loading variables, e.g.

_ = godotenv.LoadWith(shell.Bash, "my_custom.env")

Contributing

Oh yeah! Contribute, give it to me... wait, what? Contributions are very welcome of course.

Please create a PullRequest with your new features. Only well tested contributions will be accepted!

Special thanks

So, also that this code has another code base than joho's version, this project is heavily influenced by the ideas from https://github.com/joho/godotenv.

Maybe this library is also capable for your use case.

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.