A lightweight Go library for loading and managing environment variables from .env files.
- Load environment variables from .env files
- Retrieve specific environment variables from .env files
- Support for comments and empty lines
- Support for quoted values (both single and double quotes)
- Simple and easy to use API
go get github.com/cesp99/go-env
package main
import (
"fmt"
"github.com/cesp99/go-env"
)
func main() {
// Load all environment variables from .env file
err := env.LoadEnv(".env")
if err != nil {
fmt.Printf("Error loading .env file: %v\n", err)
return
}
}
package main
import (
"fmt"
"github.com/cesp99/go-env"
)
func main() {
// Get a specific environment variable from .env file
value, err := env.GetEnv("DB_HOST", ".env")
if err != nil {
fmt.Printf("Error reading environment variable: %v\n", err)
return
}
fmt.Printf("DB_HOST: %s\n", value)
}
# Database settings
DB_HOST=localhost
DB_PORT=5432
# Application settings
APP_NAME="My Application"
API_KEY='secret-key'
- Lines starting with
#
are treated as comments - Empty lines are ignored
- Supports both single and double quoted values
- Quotes are automatically trimmed from the value
- Returns appropriate errors for file operations
- Skips malformed lines without failing
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the GPL 3.0 License - see the LICENSE file for details.