@@ -8,6 +8,115 @@ A Go library that populates struct fields from environment variables. Drop-in re
88go get github.com/agentine/envstruct
99```
1010
11+ ## Quick Start
12+
13+ ``` go
14+ package main
15+
16+ import (
17+ " fmt"
18+ " log"
19+
20+ " github.com/agentine/envstruct"
21+ )
22+
23+ type Config struct {
24+ Host string ` default:"localhost" desc:"Server hostname"`
25+ Port int ` default:"8080" desc:"Server port"`
26+ Debug bool ` desc:"Enable debug mode"`
27+ }
28+
29+ func main () {
30+ var c Config
31+ if err := envstruct.Process (" APP" , &c); err != nil {
32+ log.Fatal (err)
33+ }
34+ fmt.Printf (" Listening on %s :%d \n " , c.Host , c.Port )
35+ }
36+ ```
37+
38+ Set environment variables and run:
39+
40+ ```
41+ APP_HOST=0.0.0.0 APP_PORT=9090 go run main.go
42+ # Listening on 0.0.0.0:9090
43+ ```
44+
45+ ## Supported Types
46+
47+ | Type | Example env value |
48+ | ------| -------------------|
49+ | ` string ` | ` hello ` |
50+ | ` bool ` | ` true ` , ` false ` , ` 1 ` , ` 0 ` |
51+ | ` int ` , ` int8 ` ..` int64 ` | ` 42 ` , ` -1 ` |
52+ | ` uint ` , ` uint8 ` ..` uint64 ` | ` 42 ` |
53+ | ` float32 ` , ` float64 ` | ` 3.14 ` |
54+ | ` time.Duration ` | ` 5s ` , ` 100ms ` |
55+ | ` url.URL ` / ` *url.URL ` | ` https://example.com ` |
56+ | ` []T ` (any scalar T) | ` a,b,c ` |
57+ | ` map[string]T ` | ` key1=val1,key2=val2 ` |
58+ | Custom ` Decoder ` | User-defined |
59+ | Custom ` Setter ` | envconfig compat |
60+ | ` encoding.TextUnmarshaler ` | User-defined |
61+
62+ ## Struct Tags
63+
64+ | Tag | Description |
65+ | -----| -------------|
66+ | ` env:"VAR_NAME" ` | Override env var name |
67+ | ` env:"VAR_NAME,required" ` | Mark field as required |
68+ | ` env:"-" ` | Skip field |
69+ | ` envconfig:"VAR_NAME" ` | envconfig compat tag |
70+ | ` default:"value" ` | Default if env var unset |
71+ | ` desc:"description" ` | Description for Usage() |
72+
73+ ## Nested Structs
74+
75+ Nested struct fields are flattened with ` _ ` separators:
76+
77+ ``` go
78+ type DB struct {
79+ Host string
80+ Port int
81+ }
82+ type Config struct {
83+ Database DB
84+ }
85+ // Reads APP_DATABASE_HOST, APP_DATABASE_PORT
86+ ```
87+
88+ Embedded structs are flattened without adding a prefix segment.
89+
90+ ## Usage Text
91+
92+ ``` go
93+ envstruct.Usage (" APP" , &Config{}, os.Stderr )
94+ ```
95+
96+ Outputs:
97+
98+ ```
99+ APP_HOST string [default: localhost] Server hostname
100+ APP_PORT int [default: 8080] Server port
101+ APP_DEBUG bool Enable debug mode
102+ ```
103+
104+ ## Migration from envconfig
105+
106+ envstruct is a drop-in replacement. The function signatures are identical:
107+
108+ ``` go
109+ // Before
110+ envconfig.Process (" APP" , &config)
111+ envconfig.MustProcess (" APP" , &config)
112+
113+ // After
114+ envstruct.Process (" APP" , &config)
115+ envstruct.MustProcess (" APP" , &config)
116+ ```
117+
118+ Both ` env ` and ` envconfig ` struct tags are supported for smooth migration.
119+
11120## License
12121
13122MIT
0 commit comments