-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
58 lines (46 loc) · 1019 Bytes
/
config.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
52
53
54
55
56
57
58
package goweb
import (
"bufio"
"io"
"os"
"strings"
)
const configFile = "conf/app.conf"
type Config struct {
keys map[string]string
}
func NewConfig() (*Config, error) {
cfg := &Config{
make(map[string]string),
}
file, err := os.Open(configFile)
if err != nil {
return nil, err
}
defer file.Close()
inputread := bufio.NewReader(file)
for {
input, _, err := inputread.ReadLine()
if err == io.EOF {
break
}
line := string(input)
keyval := strings.SplitN(line, "=", 2)
if len(keyval) == 2 {
cfg.keys[strings.TrimSpace(keyval[0])] = strings.TrimSpace(keyval[1])
}
}
return cfg, nil
}
func (c *Config) GetString(key string, defaultvalue string) string {
return ToString(c.keys[key], defaultvalue)
}
func (c *Config) GetInt(key string, defaultvalue int) int {
return ToInt(c.keys[key], defaultvalue)
}
func (c *Config) GetBool(key string, defaultvalue bool) bool {
return ToBool(c.keys[key], defaultvalue)
}
func (c *Config) GetMap() map[string]string {
return c.keys
}