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.
- 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
- 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
go get github.com/SameerJadav/envparse
Parse from io.Reader
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
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)
}
}
Contributions are welcome. Please open an issue or submit a pull request.
EnvParse is open-source and available under the MIT License.