@@ -21,13 +21,14 @@ which simplifies config management and improves code readability.
2121## 🚀 Features
2222
2323* Support for all common types and user-defined types
24- * Options: [ required] ( #required ) , [ expand] ( #expand ) , [ slice separator] ( #slice-separator )
2524* Configurable [ source] ( #source ) of environment variables
25+ * Options: [ required] ( #required ) , [ expand] ( #expand ) , [ slice separator] ( #slice-separator ) , [ name separator] ( #name-separator )
2626* Auto-generated [ usage message] ( #usage-message )
2727
2828## 📦 Install
2929
3030Go 1.20+
31+
3132``` shell
3233go get go-simpler.org/env
3334```
@@ -38,7 +39,7 @@ go get go-simpler.org/env
3839It loads environment variables into the given struct.
3940
4041The struct fields must have the ` env:"VAR" ` struct tag,
41- where VAR is the name of the corresponding environment variable.
42+ where ` VAR ` is the name of the corresponding environment variable.
4243Unexported fields are ignored.
4344
4445``` go
@@ -74,33 +75,35 @@ Nested struct of any depth level are supported,
7475allowing grouping of related environment variables.
7576
7677``` go
77- os.Setenv (" HTTP_PORT" , " 8080" )
78+ os.Setenv (" DB_HOST" , " localhost" )
79+ os.Setenv (" DB_PORT" , " 5432" )
7880
7981var cfg struct {
80- HTTP struct {
81- Port int ` env:"HTTP_PORT"`
82+ DB struct {
83+ Host string ` env:"DB_HOST"`
84+ Port int ` env:"DB_PORT"`
8285 }
8386}
8487if err := env.Load (&cfg, nil ); err != nil {
8588 fmt.Println (err)
8689}
8790
88- fmt.Println (cfg.HTTP .Port ) // 8080
91+ fmt.Println (cfg.DB .Host ) // localhost
92+ fmt.Println (cfg.DB .Port ) // 5432
8993```
9094
91- A nested struct can have the optional ` env:"PREFIX" ` tag.
92- In this case, the environment variables declared by its fields are prefixed with PREFIX.
93- This rule is applied recursively to all nested structs.
95+ If a nested struct has the optional ` env:"PREFIX" ` tag,
96+ the environment variables declared by its fields are prefixed with ` PREFIX ` .
9497
9598``` go
96- os.Setenv (" DBHOST " , " localhost" )
97- os.Setenv (" DBPORT " , " 5432" )
99+ os.Setenv (" DB_HOST " , " localhost" )
100+ os.Setenv (" DB_PORT " , " 5432" )
98101
99102var cfg struct {
100103 DB struct {
101104 Host string ` env:"HOST"`
102105 Port int ` env:"PORT"`
103- } ` env:"DB "`
106+ } ` env:"DB_ "`
104107}
105108if err := env.Load (&cfg, nil ); err != nil {
106109 fmt.Println (err)
@@ -112,7 +115,7 @@ fmt.Println(cfg.DB.Port) // 5432
112115
113116### Default values
114117
115- Default values can be specified using the ` default:"VALUE" ` struct tag:
118+ Default values can be specified using the ` default:"VALUE" ` struct tag.
116119
117120``` go
118121os.Unsetenv (" PORT" )
@@ -167,7 +170,7 @@ fmt.Println(cfg.Addr) // localhost:8080
167170### Slice separator
168171
169172Space is the default separator used to parse slice values.
170- It can be changed with ` Options.SliceSep ` :
173+ It can be changed with ` Options.SliceSep ` .
171174
172175``` go
173176os.Setenv (" PORTS" , " 8080,8081,8082" )
@@ -185,7 +188,7 @@ fmt.Println(cfg.Ports) // [8080 8081 8082]
185188### Name separator
186189
187190By default, environment variable names are concatenated from nested struct tags as is.
188- If ` Options.NameSep ` is not empty, it is used as the separator:
191+ If ` Options.NameSep ` is not empty, it is used as the separator.
189192
190193``` go
191194os.Setenv (" DB_HOST" , " localhost" )
@@ -216,7 +219,7 @@ type Source interface {
216219}
217220```
218221
219- Here's an example of using ` Map ` , a ` Source ` implementation useful in tests:
222+ Here's an example of using ` Map ` , a ` Source ` implementation useful in tests.
220223
221224``` go
222225m := env.Map {" PORT" : " 8080" }
@@ -234,7 +237,7 @@ fmt.Println(cfg.Port) // 8080
234237### Usage message
235238
236239The ` Usage ` function prints a usage message documenting all defined environment variables.
237- An optional usage string can be added to environment variables using the ` usage:"STRING" ` struct tag:
240+ An optional usage string can be added to environment variables with the ` usage:"STRING" ` struct tag.
238241
239242``` go
240243os.Unsetenv (" DB_HOST" )
@@ -261,7 +264,7 @@ Usage:
261264 HTTP_PORT int default 8080 http server port
262265```
263266
264- The format of the message can be customized by implementing the ` Usage([]env.Var, io.Writer, *env.Options) ` method:
267+ The format of the message can be customized by implementing the ` Usage([]env.Var, io.Writer, *env.Options) ` method.
265268
266269``` go
267270type Config struct { ... }
0 commit comments