Skip to content

Commit

Permalink
docs: fix examples
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Mar 1, 2023
1 parent c08b0f9 commit 2d26ce1
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -262,7 +302,7 @@ func main() {
}

// Print the loaded data.
fmt.Printf("%+v\n", cfg.envData)
fmt.Printf("%+v\n", cfg)
}
```

Expand Down Expand Up @@ -296,7 +336,7 @@ func main() {
}

// Print the loaded data.
fmt.Printf("%+v\n", cfg.envData)
fmt.Printf("%+v\n", cfg)
}
```

Expand Down Expand Up @@ -347,7 +387,7 @@ func main() {
}

// Print the loaded data.
fmt.Printf("%+v\n", cfg.envData)
fmt.Printf("%+v\n", cfg)
}
```

Expand Down Expand Up @@ -385,7 +425,7 @@ func main() {
}

// Print the loaded data.
fmt.Printf("%+v\n", cfg.envData)
fmt.Printf("%+v\n", cfg)
}
```

Expand Down Expand Up @@ -420,7 +460,7 @@ func main() {
}

// Print the loaded data.
fmt.Printf("%+v\n", cfg.envData)
fmt.Printf("%+v\n", cfg)
}
```

Expand Down

0 comments on commit 2d26ce1

Please sign in to comment.