From 8c4f204e58cb6973b3fbd932f5e989ee2a9fb5e6 Mon Sep 17 00:00:00 2001 From: rfyiamcool Date: Wed, 13 Sep 2023 16:02:10 +0800 Subject: [PATCH 1/2] docs: add map usage Signed-off-by: rfyiamcool --- README.md | 19 ++++++++++--------- example/main.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 example/main.go diff --git a/README.md b/README.md index 9e4cd7a..d2836e7 100644 --- a/README.md +++ b/README.md @@ -28,13 +28,14 @@ import ( ) type config struct { - Home string `env:"HOME"` - Port int `env:"PORT" envDefault:"3000"` - Password string `env:"PASSWORD,unset"` - IsProduction bool `env:"PRODUCTION"` - Hosts []string `env:"HOSTS" envSeparator:":"` - Duration time.Duration `env:"DURATION"` - TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/tmp"` + Home string `env:"HOME"` + Port int `env:"PORT" envDefault:"3000"` + Password string `env:"PASSWORD,unset"` + IsProduction bool `env:"PRODUCTION"` + Hosts []string `env:"HOSTS" envSeparator:":"` + Duration time.Duration `env:"DURATION"` + TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/tmp"` + StringInts map[string]int `env:"MAP_STRING_INT"` } func main() { @@ -50,8 +51,8 @@ func main() { You can run it like this: ```sh -$ PRODUCTION=true HOSTS="host1:host2:host3" DURATION=1s go run main.go -{Home:/your/home Port:3000 IsProduction:true Hosts:[host1 host2 host3] Duration:1s} +$ PRODUCTION=true HOSTS="host1:host2:host3" DURATION=1s MAP_STRING_INT=k1:1,k2:2 go run main.go +{Home:/your/home Port:3000 IsProduction:true Hosts:[host1 host2 host3] Duration:1s StringInts:map[k1:1 k2:2]} ``` ## Caveats diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..f0cf1aa --- /dev/null +++ b/example/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "time" + + "github.com/caarlos0/env/v9" +) + +type config struct { + Home string `env:"HOME"` + Port int `env:"PORT" envDefault:"3000"` + Password string `env:"PASSWORD,unset"` + IsProduction bool `env:"PRODUCTION"` + Hosts []string `env:"HOSTS" envSeparator:":"` + Duration time.Duration `env:"DURATION"` + TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/tmp"` + StringInts map[string]int `env:"MAP_STRING_INT"` +} + +func main() { + cfg := config{} + if err := env.Parse(&cfg); err != nil { + fmt.Printf("%+v\n", err) + } + + fmt.Printf("%+v\n", cfg) +} From 8ec83c55e5a7d81e584ee126f43a4ebc5933b414 Mon Sep 17 00:00:00 2001 From: rfyiamcool Date: Mon, 18 Sep 2023 10:18:44 +0800 Subject: [PATCH 2/2] docs: add map usage Signed-off-by: rfyiamcool --- env_test.go | 11 ++++++----- example/main.go | 28 ---------------------------- 2 files changed, 6 insertions(+), 33 deletions(-) delete mode 100644 example/main.go diff --git a/env_test.go b/env_test.go index 20e0890..74cbed4 100644 --- a/env_test.go +++ b/env_test.go @@ -1308,10 +1308,11 @@ func ExampleParse() { Foo string `env:"FOO" envDefault:"foobar"` } type config struct { - Home string `env:"HOME,required"` - Port int `env:"PORT" envDefault:"3000"` - IsProduction bool `env:"PRODUCTION"` - TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/.tmp"` + Home string `env:"HOME,required"` + Port int `env:"PORT" envDefault:"3000"` + IsProduction bool `env:"PRODUCTION"` + TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/.tmp"` + StringInts map[string]int `env:"MAP_STRING_INT" envDefault:"k1:1,k2:2"` Inner inner } os.Setenv("HOME", "/tmp/fakehome") @@ -1320,7 +1321,7 @@ func ExampleParse() { fmt.Println("failed:", err) } fmt.Printf("%+v", cfg) - // Output: {Home:/tmp/fakehome Port:3000 IsProduction:false TempFolder:/tmp/fakehome/.tmp Inner:{Foo:foobar}} + // Output: {Home:/tmp/fakehome Port:3000 IsProduction:false TempFolder:/tmp/fakehome/.tmp StringInts:map[k1:1 k2:2] Inner:{Foo:foobar}} } func ExampleParse_onSet() { diff --git a/example/main.go b/example/main.go deleted file mode 100644 index f0cf1aa..0000000 --- a/example/main.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import ( - "fmt" - "time" - - "github.com/caarlos0/env/v9" -) - -type config struct { - Home string `env:"HOME"` - Port int `env:"PORT" envDefault:"3000"` - Password string `env:"PASSWORD,unset"` - IsProduction bool `env:"PRODUCTION"` - Hosts []string `env:"HOSTS" envSeparator:":"` - Duration time.Duration `env:"DURATION"` - TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/tmp"` - StringInts map[string]int `env:"MAP_STRING_INT"` -} - -func main() { - cfg := config{} - if err := env.Parse(&cfg); err != nil { - fmt.Printf("%+v\n", err) - } - - fmt.Printf("%+v\n", cfg) -}