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:
- env
- key-value config cache/store (loaded from the .env file or set explicitly)
- default (when using structures)
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
go get -u github.com/profclems/go-dotenv
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")
The following functions and methods exist to get a value depending the Type:
Get(key string) : any
GetString(key string) : string
GetBool(key string) : bool
GetFloat64(key string) : float64
GetInt(key string) : int
GetIntSlice(key string) : []int
GetStringSlice(key string) : []string
GetTime(key string) : time.Time
GetDuration(key string) : time.Duration
isSet(key string) : bool
LookUp(key string) : (any, bool)
Set(key string, value any)
Contributions are most welcome! It could be a new feature, bug fix, refactoring or even reporting an issue.
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Added some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
Copyright © Clement Sam
This package is open-sourced software licensed under the MIT license.