forked from vrischmann/envconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
51 lines (45 loc) · 936 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package envconfig_test
import (
"fmt"
"os"
"time"
"github.com/vrischmann/envconfig"
)
func ExampleInit() {
var conf struct {
MySQL struct {
Host string
Port int
Database struct {
User string
Password string
Name string
}
}
Log struct {
Path string
Rotate bool
}
NbWorkers int
Timeout time.Duration
}
os.Setenv("MYSQL_HOST", "localhost")
os.Setenv("MYSQL_PORT", "3306")
os.Setenv("MYSQL_DATABASE_USER", "root")
os.Setenv("MYSQL_DATABASE_PASSWORD", "foobar")
os.Setenv("MYSQL_DATABASE_NAME", "default")
os.Setenv("LOG_PATH", "/var/log/foobar.log")
os.Setenv("LOG_ROTATE", "true")
os.Setenv("NBWORKERS", "10")
os.Setenv("TIMEOUT", "120s")
if err := envconfig.Init(&conf); err != nil {
fmt.Printf("err=%s\n", err)
}
fmt.Println(conf.MySQL.Database.User)
fmt.Println(conf.Log.Rotate)
fmt.Println(conf.Timeout)
// Output:
// root
// true
// 2m0s
}