aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-08-31 12:16:33 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2024-08-31 12:16:33 -0400
commit97b47df3cf0677a6600658d37446af2ecec5369f (patch)
tree4881baa1cc17f494591dd01bb2006200236e9ce6
parentUpdate readme (diff)
downloadenvelope-97b47df3cf0677a6600658d37446af2ecec5369f.zip
Update readme with usage
-rw-r--r--README.md51
1 files changed, 51 insertions, 0 deletions
diff --git a/README.md b/README.md
index 9fafc14..13c9bf5 100644
--- a/README.md
+++ b/README.md
@@ -23,3 +23,54 @@ To install Envelope, use the following command:
$ go get github.com/eugenetriguba/envelope
```
+## Usage
+
+Envelope supports the following types to load environment variables into for a Go struct:
+- `string`
+- `bool`
+- `float32`, `float64`
+- `int`, `int8`, `int16`, `int32`, `int64`
+- `uint`, `uint8`, `uint16`, `uint32`, `uint64`
+
+Given a struct with compatible types, we can use `LoadFromEnv` to populate the struct.
+
+`main.go`
+```go
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/eugenetriguba/envelope"
+)
+
+type ExampleStruct struct {
+ Field string `env:"EXAMPLE_FIELD"`
+}
+
+func main() {
+ var example ExampleStruct
+ err := envelope.LoadFromEnv(&example)
+ if err != nil {
+ fmt.Errorf("%w\n", err)
+ os.Exit(1)
+ }
+ fmt.Printf("Field value: %s\n", example.Field)
+}
+```
+
+Now, we can run the program to see what `Field` is set to:
+
+```
+$ go run main.go
+Field value:
+```
+
+When we run it without an environment variable set, we can see it is the default Go value for that type (in this case, an empty string). However, when we set the `EXAMPLE_FIELD` environment variable, we can see the struct's `Field` field has the value set as expected:
+
+```
+$ EXAMPLE_FIELD=123 go run main.go
+Field value: 123
+```
+