gotenv

Load environment variables from a .env file into your Go application.

Installation

go get github.com/OlyMahmudMugdho/gotenv

Usage

  1. Create a .env file in the root of your project with the following content:
    KEY=value
    ANOTHER_KEY=another_value
  2. In your Go application, import the package and load the environment variables:
    package main
    
    import (
        "fmt"
        "github.com/OlyMahmudMugdho/gotenv"
        "os"
    )
    
    func main() {
        // Load .env file
        gotenv.Load()
        
        // Optional: Load a specific .env file
        // gotenv.Load("path/to/your/.env")
    
        value := os.Getenv("KEY")
        fmt.Println("KEY:", value)
    }
  3. Run your application:
    go run main.go

You should see the output:

KEY: value