Skip to content

Commit

Permalink
docs: add map usage (#282)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
rfyiamcool committed Sep 18, 2023
1 parent 06b4d47 commit 6723663
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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() {
Expand Down

0 comments on commit 6723663

Please sign in to comment.