Skip to content

Commit 6723663

Browse files
authoredSep 18, 2023
docs: add map usage (#282)
* docs: add map usage Signed-off-by: rfyiamcool <rfyiamcool@163.com> * docs: add map usage Signed-off-by: rfyiamcool <rfyiamcool@163.com> --------- Signed-off-by: rfyiamcool <rfyiamcool@163.com>
1 parent 06b4d47 commit 6723663

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed
 

‎README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ import (
2828
)
2929

3030
type config struct {
31-
Home string `env:"HOME"`
32-
Port int `env:"PORT" envDefault:"3000"`
33-
Password string `env:"PASSWORD,unset"`
34-
IsProduction bool `env:"PRODUCTION"`
35-
Hosts []string `env:"HOSTS" envSeparator:":"`
36-
Duration time.Duration `env:"DURATION"`
37-
TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/tmp"`
31+
Home string `env:"HOME"`
32+
Port int `env:"PORT" envDefault:"3000"`
33+
Password string `env:"PASSWORD,unset"`
34+
IsProduction bool `env:"PRODUCTION"`
35+
Hosts []string `env:"HOSTS" envSeparator:":"`
36+
Duration time.Duration `env:"DURATION"`
37+
TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/tmp"`
38+
StringInts map[string]int `env:"MAP_STRING_INT"`
3839
}
3940

4041
func main() {
@@ -50,8 +51,8 @@ func main() {
5051
You can run it like this:
5152

5253
```sh
53-
$ PRODUCTION=true HOSTS="host1:host2:host3" DURATION=1s go run main.go
54-
{Home:/your/home Port:3000 IsProduction:true Hosts:[host1 host2 host3] Duration:1s}
54+
$ PRODUCTION=true HOSTS="host1:host2:host3" DURATION=1s MAP_STRING_INT=k1:1,k2:2 go run main.go
55+
{Home:/your/home Port:3000 IsProduction:true Hosts:[host1 host2 host3] Duration:1s StringInts:map[k1:1 k2:2]}
5556
```
5657

5758
## Caveats

‎env_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1308,10 +1308,11 @@ func ExampleParse() {
13081308
Foo string `env:"FOO" envDefault:"foobar"`
13091309
}
13101310
type config struct {
1311-
Home string `env:"HOME,required"`
1312-
Port int `env:"PORT" envDefault:"3000"`
1313-
IsProduction bool `env:"PRODUCTION"`
1314-
TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/.tmp"`
1311+
Home string `env:"HOME,required"`
1312+
Port int `env:"PORT" envDefault:"3000"`
1313+
IsProduction bool `env:"PRODUCTION"`
1314+
TempFolder string `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/.tmp"`
1315+
StringInts map[string]int `env:"MAP_STRING_INT" envDefault:"k1:1,k2:2"`
13151316
Inner inner
13161317
}
13171318
os.Setenv("HOME", "/tmp/fakehome")
@@ -1320,7 +1321,7 @@ func ExampleParse() {
13201321
fmt.Println("failed:", err)
13211322
}
13221323
fmt.Printf("%+v", cfg)
1323-
// Output: {Home:/tmp/fakehome Port:3000 IsProduction:false TempFolder:/tmp/fakehome/.tmp Inner:{Foo:foobar}}
1324+
// Output: {Home:/tmp/fakehome Port:3000 IsProduction:false TempFolder:/tmp/fakehome/.tmp StringInts:map[k1:1 k2:2] Inner:{Foo:foobar}}
13241325
}
13251326

13261327
func ExampleParse_onSet() {

0 commit comments

Comments
 (0)
Please sign in to comment.