Installation
go get github.com/OlyMahmudMugdho/gotenv
Usage
- Create a
.env
file in the root of your project with the following content:KEY=value ANOTHER_KEY=another_value
- 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) }
- Run your application:
go run main.go
You should see the output:
KEY: value