-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
47 lines (37 loc) · 836 Bytes
/
main.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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/tkanos/gonfig"
)
type Configuration struct {
Port int `env:"APP_PORT"`
Connection_String string
}
func main() {
// mock env variable
os.Setenv("Connection_String", "test")
os.Setenv("APP_PORT", "8081")
configuration := Configuration{}
err := gonfig.GetConf(getFileName(), &configuration)
if err != nil {
fmt.Println(err)
os.Exit(500)
}
fmt.Println(configuration.Port)
fmt.Println(configuration.Connection_String)
}
func getFileName() string {
env := os.Getenv("ENV")
if len(env) == 0 {
env = "development"
}
filename := []string{"config/", "config.", env, ".json"}
_, dirname, _, _ := runtime.Caller(0)
filePath := path.Join(filepath.Dir(dirname), strings.Join(filename, ""))
return filePath
}