https://github.com/sameerjadav/envparse
Go package designed for efficiently parsing environment variables from .env files. It provides a straightforward and performant way to load environment variables into your Go applications.
https://github.com/sameerjadav/envparse
env-parser environment-variables go golang golang-package
Last synced: 5 months ago
JSON representation
Go package designed for efficiently parsing environment variables from .env files. It provides a straightforward and performant way to load environment variables into your Go applications.
- Host: GitHub
- URL: https://github.com/sameerjadav/envparse
- Owner: SameerJadav
- License: mit
- Created: 2024-08-01T10:44:53.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-09T08:41:38.000Z (almost 2 years ago)
- Last Synced: 2024-08-09T10:03:02.319Z (almost 2 years ago)
- Topics: env-parser, environment-variables, go, golang, golang-package
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EnvParse
[](https://pkg.go.dev/github.com/SameerJadav/envparse) [](https://github.com/SameerJadav/envparse/actions/workflows/tests.yml)
EnvParse is a Go package designed for efficiently parsing environment variables from `.env` files. It provides a straightforward and performant way to load environment variables into your Go applications.
## Features
- Parse environment files from any `io.Reader` source
- Parse environment files directly from a file
- Handling of quoted values (double quotes, single quotes, and backticks)
- Variable expansion in non-quoted and double-quoted values
- Error reporting with line numbers for invalid syntax
## Parsing Details
- Double-quoted values are unescaped, including unicode characters
- Single-quoted and backtick-quoted values are treated as literal strings
- Variable expansion is performed in non-quoted and double-quoted values
- Commented lines (lines prefixed with `#`), invalid lines (lines that are not comments and do not have an `=` sign), and lines with empty key will not be parsed
- Inline comments are removed from the value. If you want `#` in your value, you should quote it (e.g., `KEY="VALUE#with hash"`)
- Does not support multiline values
## Installation
```shell
go get github.com/SameerJadav/envparse
```
## Usage
Parse from `io.Reader`
```go
package main
import (
"log"
"os"
"github.com/SameerJadav/envparse"
)
func main() {
file, err := os.Open(".env")
if err != nil {
log.Fatal(err)
}
defer file.Close()
env, err := envparse.Parse(file)
if err != nil {
log.Fatal(err)
}
for key, value := range env {
os.Setenv(key, value)
}
}
```
Parse from File
```go
package main
import (
"log"
"os"
"github.com/SameerJadav/envparse"
)
func main() {
env, err := envparse.ParseFile(".env")
if err != nil {
log.Fatal(err)
}
for key, value := range env {
os.Setenv(key, value)
}
}
```
## Contributing
Contributions are welcome. Please open an issue or submit a pull request.
## License
EnvParse is open-source and available under the [MIT License](./LICENSE).