Documentation
¶
Overview ¶
Package godenv is a tiny module that parses .env files.
Motivation ¶
We took inspiration from the godotenv (https://github.com/joho/godotenv) repository. The goal we pursued was to write a parser without using regular expressions but with a lexer/parser approach.
The current specification of .env files format is listed in SPECIFICATION.md (https://github.com/youla-dev/godenv/blob/main/SPECIFICATION.md).
If you are curious about learning more about the approach, see the following links:
- https://en.wikipedia.org/wiki/Abstract_syntax_tree
- https://en.wikipedia.org/wiki/Lexical_analysis
- https://en.wikipedia.org/wiki/Parsing#Parser
Usage ¶
Let's assume, you have a .env file with the following content:
HTTP_LISTEN=":8080" LOG_LEVEL="info"
You can easily open the file and parse its content into map[string]string:
f, err := os.Open(".env")
if err != nil {
panic(err)
}
defer f.Close()
vars, err := godenv.Parse(f)
if err != nil {
panic(err)
}
fmt.Println(vars)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse reads an env file from io.Reader, returning a map of keys and values.
Example ¶
package main
import (
"bytes"
"fmt"
"github.com/youla-dev/godenv"
)
func main() {
envContent := `VARIABLE_1='This is variable 1'
# This is comment
VARIABLE_2="This is variable 2"
VARIABLE_TAB_1='Tab is not escaped\t'
VARIABLE_TAB_2="Tab is escaped\t"`
buf := bytes.NewBufferString(envContent)
result, err := godenv.Parse(buf)
if err != nil {
panic(err)
}
fmt.Println(result)
}
Output: map[VARIABLE_1:This is variable 1 VARIABLE_2:This is variable 2 VARIABLE_TAB_1:Tab is not escaped\t VARIABLE_TAB_2:Tab is escaped ]
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
ast
Package ast declares the types used to represent syntax trees for the .env file.
|
Package ast declares the types used to represent syntax trees for the .env file. |
|
parser
Package parser implements a parser for the .env files.
|
Package parser implements a parser for the .env files. |
|
token
Package token defines constants representing the lexical tokens of the .env file.
|
Package token defines constants representing the lexical tokens of the .env file. |