profclems / go-dotenv

A fast and minimalist library for loading .env files in Go. Zero Allocation Get and Set methods
MIT License
22 stars 3 forks source link
configuration environment-variables go golang golang-library hacktoberfest hacktoberfest2021

Dotenv Go Report Card PkgGoDev

Dotenv is a minimal Go Library for reading .env configuration files.

Dotenv reads config in the following order. Each item takes precedence over the item below it:

The config cache store is set on first read operation.

BenchmarkDotenv_Load-12            57186             19774 ns/op           17477 B/op         89 allocs/op
BenchmarkDotenv_instance/Get-12                         30091465                39.93 ns/op            0 B/op          0 allocs/op
BenchmarkDotenv_instance/Get_NotExist-12                24608632                48.31 ns/op            0 B/op          0 allocs/op
BenchmarkDotenv_instance/Set-12                         52317692                23.51 ns/op            0 B/op          0 allocs/op
BenchmarkDotenv_global/Get-12                           25803398                46.73 ns/op            0 B/op          0 allocs/op
BenchmarkDotenv_global/Get_NotExist-12                  20868642                59.51 ns/op            0 B/op          0 allocs/op
BenchmarkDotenv_global/Set-12                           37934266                32.20 ns/op            0 B/op          0 allocs/op

Installation

go get -u github.com/profclems/go-dotenv

Usage

Assuming you have a .env file in the current directory with the following values

S3_BUCKET=yours3bucket
SECRET_KEY=yoursecretKey
PRIORITY_LEVEL=2

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

package main

import (
    "log"

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

func main() {
  // .env - It will search for the .env file in the current directory and load it. 
  // You can explicitly set config file with dotenv.SetConfigFile("path/to/file.env")
  err := dotenv.Load()
  if err != nil {
    log.Fatalf("Error loading .env file: %v", err)
  }

  s3Bucket := dotenv.GetString("S3_BUCKET")
  secretKey := dotenv.GetString("SECRET_KEY")
  priorityLevel := dotenv.GetInt("PRIORITY_LEVEL")

  // now do something with s3 or whatever
}

Comments and exports are supported:

# This is a comment
S3_BUCKET=yours3bucket
SECRET_KEY=yoursecretKey # This is also a comment
export PRIORITY_LEVEL=2

All the above examples use the global DotEnv instance. You can instantiate a new Dotenv instance:

cfg := dotenv.New()
cfg.SetConfigFile("path/to/.env")
err := cfg.Load()
if err != nil {
    log.Fatalf("Error loading .env file: %v", err)
}

val := cfg.GetString("SOME_ENV")

Getting Values From DotEnv

The following functions and methods exist to get a value depending the Type:

Contributing

Contributions are most welcome! It could be a new feature, bug fix, refactoring or even reporting an issue.

License

Copyright © Clement Sam

This package is open-sourced software licensed under the MIT license.