From 2d26ce1cd4c10a7fa055c643cfde3a4a295f8e6d Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Wed, 1 Mar 2023 11:01:47 -0300 Subject: [PATCH] docs: fix examples Signed-off-by: Carlos A Becker --- README.md | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 39aebd2..b8eb920 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,46 @@ $ SECRET=/tmp/secret \ ## Options +### Use field names as environment variables by default + +If you don't want to set the `env` tag on every field, you can use the +`UseFieldNameByDefault` option. + +It will use the field name as environment variable name. + +Here's an example: + + +```go +package main + +import ( + "fmt" + "log" + + "github.com/caarlos0/env/v7" +) + +type Config struct { + Username string // will use $USERNAME + Password string // will use $PASSWORD + UserFullName string // will use $USER_FULL_NAME +} + +func main() { + cfg := &Config{} + opts := &env.Options{UseFieldNameByDefault: true} + + // Load env vars. + if err := env.Parse(cfg, opts); err != nil { + log.Fatal(err) + } + + // Print the loaded data. + fmt.Printf("%+v\n", cfg) +} +``` + ### Environment By setting the `Options.Environment` map you can tell `Parse` to add those `keys` and `values` @@ -262,7 +302,7 @@ func main() { } // Print the loaded data. - fmt.Printf("%+v\n", cfg.envData) + fmt.Printf("%+v\n", cfg) } ``` @@ -296,7 +336,7 @@ func main() { } // Print the loaded data. - fmt.Printf("%+v\n", cfg.envData) + fmt.Printf("%+v\n", cfg) } ``` @@ -347,7 +387,7 @@ func main() { } // Print the loaded data. - fmt.Printf("%+v\n", cfg.envData) + fmt.Printf("%+v\n", cfg) } ``` @@ -385,7 +425,7 @@ func main() { } // Print the loaded data. - fmt.Printf("%+v\n", cfg.envData) + fmt.Printf("%+v\n", cfg) } ``` @@ -420,7 +460,7 @@ func main() { } // Print the loaded data. - fmt.Printf("%+v\n", cfg.envData) + fmt.Printf("%+v\n", cfg) } ```