A Go package to load environment variables from .env files.
go get github.com/fahmiidris/envCreate a .env file in the root of your project:
SECRET_KEY=YOURSECRETKEYVALUEUse env.Load() to load variables from a .env file. By default, it looks for a file named .env in the current directory. This will not overwrite variables that are already set in the environment.
package main
import (
"log"
"os"
"github.com/fahmiidris/env"
)
func main() {
err := env.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
s3Bucket := os.Getenv("S3_BUCKET")
secretKey := os.Getenv("SECRET_KEY")
// Now do something with s3Bucket and secretKey
}If you want to overwrite existing environment variables, use env.Overload():
err := env.Overload()You can also specify multiple files:
err := env.Load(".env", ".env.local")If you just want to read the variables into a map without modifying the environment, use env.Read():
myEnv, err := env.Read()
if err != nil {
log.Fatal(err)
}
s3Bucket := myEnv["S3_BUCKET"]- Standard format:
KEY=VALUE - Comments: Lines starting with
#and inline comments are supported. - Exports: Supports
export KEY=VALUEsyntax. - Quoting:
- Single quotes (
') for literal values. - Double quotes (
") for values with escape sequences (e.g.,\n,\t).
- Single quotes (
- Multiple Files: Load from multiple files sequentially.