Code Monkey home page Code Monkey logo

go-dotenv's Introduction

Go Dotenv

Description:

Go Dotenv is a lightweight Go package designed to simplify the configuration of your Go applications by loading environment variables from a .env file (or any file of your choice). It provides a flexible structure, allowing you to customize the configuration process based on your application's needs.

Note: The default env file is a ".env" file at the root of the project and the default logger writes to os.std.Out

Features:

  • Simple Configuration: Easily load environment variables from a specified .env file or use default values.
  • Logger Integration: Seamlessly integrate with your existing logging infrastructure by accepting a *log.Logger as a parameter.
  • Error Handling: Provides clear error messages for quick debugging.
  • Customization: Customize the configuration process by specifying a filename, logger, or both, depending on your application requirements.

Installation

go get -u github.com/tonie-ng/go-dotenv

Usage

There are various ways to use this package.

  1. Using the default values for the filename and logger. Please ensure to have a .env file at the root directory of your project
package main

import (
	"fmt"
	"os"

	"github.com/tonie-ng/go-dotenv"
)

func main() {
    
    // loads the env variables using the defaults while ignoring the return values.
    dotenv.Config()

    envVariable := os.Getenv("MY_ENV_KEY")
    
    // This prints the value assigned to the MY_ENV_KEY
    fmt.Println(envVariable)
}

or

package main

import (
	"fmt"

	"github.com/tonie-ng/go-dotenv"
)

func main() {


    // dotenv.Config() returns a map of all the environment variables provided in the .env file and an err if any.
	envVars, err := dotenv.Config()

	if err != nil {
		fmt.Println(err)
	}
    
	for _, v := range envVars {
		fmt.Println(v)
	}
}
  1. Providing a value for either the logger or the filename. (If one is provided the other is assigned to the default.)
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/tonie-ng/go-dotenv"
)

func main() {

	
	// We provide a string here and this is assigned to the filename while the logger uses the dafault.
	dotenv.Config("env")


	// To assign the logger to a value instead, we do:
	logger := log.New(os.Stdout, "Example logger", log.LstdFlags)

	dotenv.Config(logger)

	
	// You can also assign logger to nil by doing. This indicates that you dont want a logger.
	dotenv.Config(nil)

    // prints the value of the MY_ENV_KEY provided in the env file
	fmt.Println(os.Getenv("MY_ENV_KEY"))
}
  1. Providing a logger and a filename.
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/tonie-ng/go-dotenv"
)

func main() {

	filename := "sample.env"

	logger := log.New(os.Stdout, "Example logger", log.LstdFlags)

	dotenv.Config(filename, logger)

	// You can also assign logger to nil by doing. This indicates that you don't want a logger.
	dotenv.Config(filename, nil)

	fmt.Println(os.Getenv("MY_ENV_KEY"))
}

Contibutions

Contributions to this project are welcome, plese refer to the contibution guide.

TODO

  • Add support for string recognition in env files. ie
TEST_ENV="envvarwithstrings" should output => envvarwithstrings but without the double quotes

Note: I should probably raise issues instead of updating this TODO list :/.

go-dotenv's People

Contributors

tonie-ng avatar

Watchers

 avatar

go-dotenv's Issues

Incorrect parsing.

The algo for handling the env parsing is wrong. In it's current state it parses it doesn't regard cases where an env value might have an = sign e.g

DB_CONN_STRING="postgres://postgres:passw@localhost:5432/nest-db?sslmode=disable"

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.