@@ -31,7 +31,7 @@ go get go-simpler.org/env
31
31
* Dependency-free
32
32
* Per-variable options: [ required] ( #required ) , [ expand] ( #expand )
33
33
* Global options: [ source] ( #source ) , [ prefix] ( #prefix ) , [ slice separator] ( #slice-separator )
34
- * Auto-generated [ usage message] ( #usage-on-error )
34
+ * Auto-generated [ usage message] ( #usage-message )
35
35
36
36
## 📋 Usage
37
37
@@ -48,10 +48,11 @@ var cfg struct {
48
48
Port int ` env:"PORT"`
49
49
}
50
50
if err := env.Load (&cfg); err != nil {
51
- // handle error
51
+ fmt. Println (err)
52
52
}
53
53
54
- fmt.Println (cfg.Port ) // 8080
54
+ fmt.Println (cfg.Port )
55
+ // Output: 8080
55
56
```
56
57
57
58
### Supported types
@@ -78,34 +79,31 @@ var cfg struct {
78
79
}
79
80
}
80
81
if err := env.Load (&cfg); err != nil {
81
- // handle error
82
+ fmt. Println (err)
82
83
}
83
84
84
- fmt.Println (cfg.HTTP .Port ) // 8080
85
+ fmt.Println (cfg.HTTP .Port )
86
+ // Output: 8080
85
87
```
86
88
87
89
### Default values
88
90
89
- Default values can be specified either using the ` default ` struct tag (has a
90
- higher priority) or by initializing the struct fields directly.
91
+ Default values can be specified using the ` default ` struct tag:
91
92
92
93
``` go
93
- cfg := struct {
94
- Host string ` env:"HOST" default:"localhost"` // either use the `default` tag...
95
- Port int ` env:"PORT"`
96
- }{
97
- Port : 8080 , // ...or initialize the struct field directly.
94
+ os.Unsetenv (" PORT" )
95
+
96
+ var cfg struct {
97
+ Port int ` env:"PORT" default:"8080"`
98
98
}
99
99
if err := env.Load (&cfg); err != nil {
100
- // handle error
100
+ fmt. Println (err)
101
101
}
102
102
103
- fmt.Println (cfg.Host ) // localhost
104
- fmt. Println (cfg. Port ) // 8080
103
+ fmt.Println (cfg.Port )
104
+ // Output: 8080
105
105
```
106
106
107
- ## 🔧 Options
108
-
109
107
### Per-variable options
110
108
111
109
The name of the environment variable can be followed by comma-separated options
@@ -117,8 +115,8 @@ Use the `required` option to mark the environment variable as required. In case
117
115
no such variable is found, an error of type ` NotSetError ` will be returned.
118
116
119
117
``` go
120
- // os.Setenv ("HOST", "localhost ")
121
- // os.Setenv ("PORT", "8080 ")
118
+ os.Unsetenv (" HOST" )
119
+ os.Unsetenv (" PORT" )
122
120
123
121
var cfg struct {
124
122
Host string ` env:"HOST,required"`
@@ -127,9 +125,11 @@ var cfg struct {
127
125
if err := env.Load (&cfg); err != nil {
128
126
var notSetErr *env.NotSetError
129
127
if errors.As (err, ¬SetErr) {
130
- fmt.Println (notSetErr.Names ) // [HOST PORT]
128
+ fmt.Println (notSetErr.Names )
131
129
}
132
130
}
131
+
132
+ // Output: [HOST PORT]
133
133
```
134
134
135
135
#### Expand
@@ -142,13 +142,14 @@ os.Setenv("PORT", "8080")
142
142
os.Setenv (" ADDR" , " localhost:${PORT}" )
143
143
144
144
var cfg struct {
145
- Addr string ` env:"ADDR,expand"`
145
+ Addr string ` env:"ADDR,expand"`
146
146
}
147
147
if err := env.Load (&cfg); err != nil {
148
- // handle error
148
+ fmt. Println (err)
149
149
}
150
150
151
- fmt.Println (cfg.Addr ) // localhost:8080
151
+ fmt.Println (cfg.Addr )
152
+ // Output: localhost:8080
152
153
```
153
154
154
155
### Global options
@@ -177,10 +178,11 @@ var cfg struct {
177
178
Port int ` env:"PORT"`
178
179
}
179
180
if err := env.Load (&cfg, env.WithSource (m)); err != nil {
180
- // handle error
181
+ fmt. Println (err)
181
182
}
182
183
183
- fmt.Println (cfg.Port ) // 8080
184
+ fmt.Println (cfg.Port )
185
+ // Output: 8080
184
186
```
185
187
186
188
#### Prefix
@@ -195,10 +197,11 @@ var cfg struct {
195
197
Port int ` env:"PORT"`
196
198
}
197
199
if err := env.Load (&cfg, env.WithPrefix (" APP_" )); err != nil {
198
- // handle error
200
+ fmt. Println (err)
199
201
}
200
202
201
- fmt.Println (cfg.Port ) // 8080
203
+ fmt.Println (cfg.Port )
204
+ // Output: 8080
202
205
```
203
206
204
207
#### Slice separator
@@ -213,45 +216,35 @@ var cfg struct {
213
216
Ports []int ` env:"PORTS"`
214
217
}
215
218
if err := env.Load (&cfg, env.WithSliceSeparator (" ;" )); err != nil {
216
- // handle error
219
+ fmt. Println (err)
217
220
}
218
221
219
- fmt.Println (cfg.Ports [0 ]) // 8080
220
- fmt.Println (cfg.Ports [1 ]) // 8081
221
- fmt.Println (cfg.Ports [2 ]) // 8082
222
+ fmt.Println (cfg.Ports )
223
+ // Output: [8080 8081 8082]
222
224
```
223
225
224
- #### Usage on error
226
+ ### Usage message
225
227
226
- ` env ` supports printing an auto-generated usage message the same way the ` flag `
227
- package does it. It will be printed if the ` WithUsageOnError ` option is
228
- provided and an error occurs while loading environment variables:
228
+ ` env ` supports printing an auto-generated usage message the same way the ` flag ` package does it.
229
229
230
230
``` go
231
- // os.Setenv("DB_HOST", "localhost")
232
- // os.Setenv("DB_PORT", "5432")
233
-
234
- cfg := struct {
231
+ var cfg struct {
235
232
DB struct {
236
233
Host string ` env:"DB_HOST,required" desc:"database host"`
237
234
Port int ` env:"DB_PORT,required" desc:"database port"`
238
235
}
239
- HTTPPort int ` env:"HTTP_PORT" desc:"http server port"`
240
- Timeouts []time.Duration ` env:"TIMEOUTS" desc:"timeout steps"`
241
- }{
242
- HTTPPort : 8080 ,
243
- Timeouts : []time.Duration {1 * time.Second , 2 * time.Second , 3 * time.Second },
236
+ HTTPPort int ` env:"HTTP_PORT" default:"8080" desc:"http server port"`
244
237
}
245
- if err := env.Load (&cfg, env.WithUsageOnError (os.Stdout )); err != nil {
246
- // handle error
238
+ if err := env.Load (&cfg); err != nil {
239
+ fmt.Println (err)
240
+ env.Usage (&cfg, os.Stdout )
247
241
}
248
242
249
- // Output:
243
+ // Output: env: [DB_HOST DB_PORT] are required but not set
250
244
// Usage:
251
- // DB_HOST string required database host
252
- // DB_PORT int required database port
253
- // HTTP_PORT int default 8080 http server port
254
- // TIMEOUTS []time.Duration default [1s 2s 3s] timeout steps
245
+ // DB_HOST string required database host
246
+ // DB_PORT int required database port
247
+ // HTTP_PORT int default 8080 http server port
255
248
```
256
249
257
250
[ 1 ] : https://12factor.net/config
0 commit comments