| 450 | } |
| 451 | |
| 452 | func ExampleDecode() { |
| 453 | type Example struct { |
| 454 | // A string field, without any default |
| 455 | String string `env:"EXAMPLE_STRING"` |
| 456 | |
| 457 | // A uint16 field, with a default value of 100 |
| 458 | Uint16 uint16 `env:"EXAMPLE_UINT16,default=100"` |
| 459 | } |
| 460 | |
| 461 | os.Setenv("EXAMPLE_STRING", "an example!") |
| 462 | |
| 463 | var e Example |
| 464 | if err := Decode(&e); err != nil { |
| 465 | panic(err) |
| 466 | } |
| 467 | |
| 468 | // If TEST_STRING is set, e.String will contain its value |
| 469 | fmt.Println(e.String) |
| 470 | |
| 471 | // If TEST_UINT16 is set, e.Uint16 will contain its value. |
| 472 | // Otherwise, it will contain the default value, 100. |
| 473 | fmt.Println(e.Uint16) |
| 474 | |
| 475 | // Output: |
| 476 | // an example! |
| 477 | // 100 |
| 478 | } |
| 479 | |
| 480 | //// Export tests |
| 481 | |