4
4
[ ![ Coverage Status] ( https://img.shields.io/codecov/c/gh/caarlos0/env.svg?logo=codecov&style=for-the-badge )] ( https://codecov.io/gh/caarlos0/env )
5
5
[ ![ ] ( http://img.shields.io/badge/godoc-reference-5272B4.svg?style=for-the-badge )] ( https://pkg.go.dev/github.com/caarlos0/env/v8 )
6
6
7
- A simple and zero-dependencies library to parse environment variables into structs.
7
+ A simple and zero-dependencies library to parse environment variables into
8
+ ` struct ` s.
8
9
9
10
## Example
10
11
@@ -125,8 +126,10 @@ for more info.
125
126
126
127
Env supports by default anything that implements the ` TextUnmarshaler ` interface.
127
128
That includes things like ` time.Time ` for example.
128
- The upside is that depending on the format you need, you don't need to change anything.
129
- The downside is that if you do need time in another format, you'll need to create your own type.
129
+ The upside is that depending on the format you need, you don't need to change
130
+ anything.
131
+ The downside is that if you do need time in another format, you'll need to
132
+ create your own type.
130
133
131
134
Its fairly straightforward:
132
135
@@ -148,19 +151,28 @@ And then you can parse `Config` with `env.Parse`.
148
151
149
152
## Required fields
150
153
151
- The ` env ` tag option ` required ` (e.g., ` env:"tagKey,required" ` ) can be added to ensure that some environment variable is set.
152
- In the example above, an error is returned if the ` config ` struct is changed to:
154
+ The ` env ` tag option ` required ` (e.g., ` env:"tagKey,required" ` ) can be added to
155
+ ensure that some environment variable is set. In the example above, an error is
156
+ returned if the ` config ` struct is changed to:
153
157
154
158
``` go
155
159
type config struct {
156
160
SecretKey string ` env:"SECRET_KEY,required"`
157
161
}
158
162
```
159
163
164
+ > ** Warning**
165
+ >
166
+ > Note that being set is not the same as being empty.
167
+ > If the variable is set, but empty, the field will have its type's default
168
+ > value.
169
+ > This also means that custom parser funcs will not be invoked.
170
+
160
171
## Not Empty fields
161
172
162
- While ` required ` demands the environment variable to be set, it doesn't check its value.
163
- If you want to make sure the environment is set and not empty, you need to use the ` notEmpty ` tag option instead (` env:"SOME_ENV,notEmpty" ` ).
173
+ While ` required ` demands the environment variable to be set, it doesn't check
174
+ its value. If you want to make sure the environment is set and not empty, you
175
+ need to use the ` notEmpty ` tag option instead (` env:"SOME_ENV,notEmpty" ` ).
164
176
165
177
Example:
166
178
@@ -186,9 +198,9 @@ type config struct {
186
198
## From file
187
199
188
200
The ` env ` tag option ` file ` (e.g., ` env:"tagKey,file" ` ) can be added
189
- to in order to indicate that the value of the variable shall be loaded from a file. The path of that file is given
190
- by the environment variable associated with it
191
- Example below
201
+ in order to indicate that the value of the variable shall be loaded from a
202
+ file.
203
+ The path of that file is given by the environment variable associated with it:
192
204
193
205
``` go
194
206
package main
@@ -269,9 +281,11 @@ func main() {
269
281
270
282
### Environment
271
283
272
- By setting the ` Options.Environment ` map you can tell ` Parse ` to add those ` keys ` and ` values `
273
- as env vars before parsing is done. These envs are stored in the map and never actually set by ` os.Setenv ` .
274
- This option effectively makes ` env ` ignore the OS environment variables: only the ones provided in the option are used.
284
+ By setting the ` Options.Environment ` map you can tell ` Parse ` to add those
285
+ ` keys ` and ` values ` as ` env ` vars before parsing is done.
286
+ These ` envs ` are stored in the map and never actually set by ` os.Setenv ` .
287
+ This option effectively makes ` env ` ignore the OS environment variables: only
288
+ the ones provided in the option are used.
275
289
276
290
This can make your testing scenarios a bit more clean and easy to handle.
277
291
@@ -307,8 +321,8 @@ func main() {
307
321
308
322
### Changing default tag name
309
323
310
- You can change what tag name to use for setting the env vars by setting the ` Options.TagName `
311
- variable.
324
+ You can change what tag name to use for setting the env vars by setting the
325
+ ` Options.TagName ` variable.
312
326
313
327
For example
314
328
@@ -391,7 +405,8 @@ func main() {
391
405
392
406
### On set hooks
393
407
394
- You might want to listen to value sets and, for example, log something or do some other kind of logic.
408
+ You might want to listen to value sets and, for example, log something or do
409
+ some other kind of logic.
395
410
You can do this by passing a ` OnSet ` option:
396
411
397
412
``` go
@@ -429,7 +444,8 @@ func main() {
429
444
430
445
## Making all fields to required
431
446
432
- You can make all fields that don't have a default value be required by setting the ` RequiredIfNoDef: true ` in the ` Options ` .
447
+ You can make all fields that don't have a default value be required by setting
448
+ the ` RequiredIfNoDef: true ` in the ` Options ` .
433
449
434
450
For example
435
451
@@ -464,8 +480,10 @@ func main() {
464
480
465
481
## Defaults from code
466
482
467
- You may define default value also in code, by initialising the config data before it's filled by ` env.Parse ` .
468
- Default values defined as struct tags will overwrite existing values during Parse.
483
+ You may define default value also in code, by initialising the config data
484
+ before it's filled by ` env.Parse ` .
485
+ Default values defined as struct tags will overwrite existing values during
486
+ Parse.
469
487
470
488
``` go
471
489
package main
0 commit comments