Categorygithub.com/tonie-ng/go-dotenv
repositorypackage
0.1.2
Repository: https://github.com/tonie-ng/go-dotenv.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

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 :/.