Skip to content

Commit db80fdf

Browse files
committed
Add example in README.md
Signed-off-by: Ulysses Souza <[email protected]>
1 parent d282730 commit db80fdf

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,68 @@ It's meant to be configurable through a simple API that lets you define your own
1313
go get -u github.com/ulyssessouza/envlang
1414
```
1515

16+
### Usage
17+
18+
Given the following environment file `input.env`:
19+
```env
20+
FOO=foo
21+
BAR="bar"
22+
BAZ = 1
23+
EMPTY=
24+
UNSET
25+
26+
VAR1=$FOO
27+
VAR2=${BAR}
28+
VAR3=${EMPTY:-default_value_for_empty}
29+
VAR4=${UNSET-default_value_for_unset}
30+
```
31+
32+
Example code:
33+
```go
34+
package main
35+
36+
import (
37+
"fmt"
38+
"os"
39+
40+
"github.com/ulyssessouza/envlang"
41+
"github.com/ulyssessouza/envlang/dao"
42+
)
43+
44+
func main() {
45+
file, err := os.Open("input.env")
46+
if err != nil {
47+
return
48+
}
49+
d := dao.NewDefaultDaoFromMap(nil)
50+
vars := envlang.GetVariablesFromInputStream(d, file)
51+
fmt.Printf("FOO=%q\n", *vars["FOO"])
52+
fmt.Printf("BAR=%q\n", *vars["BAR"])
53+
fmt.Printf("BAZ=%q\n", *vars["BAZ"])
54+
fmt.Printf("EMPTY=%q\n", *vars["EMPTY"]) // Use %q to print empty strings
55+
fmt.Printf("UNSET=%q\n", vars["UNSET"])
56+
fmt.Printf("VAR1=%q\n", *vars["VAR1"])
57+
fmt.Printf("VAR2=%q\n", *vars["VAR2"])
58+
fmt.Printf("VAR3=%q\n", *vars["VAR3"])
59+
fmt.Printf("VAR4=%q\n", *vars["VAR4"])
60+
}
61+
```
62+
63+
Output:
64+
```
65+
FOO="foo"
66+
BAR="bar"
67+
BAZ="1"
68+
EMPTY=""
69+
UNSET=%!q(*string=<nil>)
70+
VAR1="foo"
71+
VAR2="bar"
72+
VAR3="default_value_for_empty"
73+
VAR4="default_value_for_unset"
74+
```
75+
76+
Please note that it used `%q` to print empty strings, as the default `fmt.Printf` will not print them.
77+
1678
## License
1779

1880
This project is licensed under the [MIT](LICENSE) License - see the LICENSE.md file for details

0 commit comments

Comments
 (0)