https://github.com/cesp99/go-env
Lightweight Go library to handle .env files
https://github.com/cesp99/go-env
Last synced: 2 months ago
JSON representation
Lightweight Go library to handle .env files
- Host: GitHub
- URL: https://github.com/cesp99/go-env
- Owner: cesp99
- License: gpl-3.0
- Created: 2025-02-04T13:08:49.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-02-05T15:35:39.000Z (4 months ago)
- Last Synced: 2025-03-04T07:12:56.398Z (3 months ago)
- Language: Go
- Size: 20.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Go env
A lightweight Go library for loading and managing environment variables from .env files.
## Features
- 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## Installation
```bash
go get github.com/cesp99/go-env
```## Usage
### Loading Environment Variables
```go
package mainimport (
"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
}
}
```### Getting Specific Environment Variables
```go
package mainimport (
"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)
}
```### Example .env File
```env
# Database settings
DB_HOST=localhost
DB_PORT=5432# Application settings
APP_NAME="My Application"
API_KEY='secret-key'
```## Features
### Comments and Empty Lines
- Lines starting with `#` are treated as comments
- Empty lines are ignored### Quoted Values
- Supports both single and double quoted values
- Quotes are automatically trimmed from the value### Error Handling
- Returns appropriate errors for file operations
- Skips malformed lines without failing## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
This project is licensed under the GPL 3.0 License - see the [LICENSE](./LICENSE.txt) file for details.