From ac5d00f22d4a7433349a24573bbbc39042dbcfdc Mon Sep 17 00:00:00 2001 From: "fengyun.rui" Date: Mon, 18 Sep 2023 20:49:14 +0800 Subject: [PATCH] docs: add map usage (#282) * docs: add map usage Signed-off-by: rfyiamcool * docs: add map usage Signed-off-by: rfyiamcool --------- Signed-off-by: rfyiamcool --- README.md | 19 ++++++++++--------- env_test.go | 11 ++++++----- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 9e4cd7af..d2836e79 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/env_test.go b/env_test.go index 20e08902..74cbed4d 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() {