-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.go
31 lines (28 loc) · 865 Bytes
/
cfg.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
// bitbucket.org/gotamer/cfg is a json configuration package
// * You define your config items in your application as a struct.
// * A json config file template from your struct is saved on the first run
// * You can save runtime modifications to the config
// See doc.go for an example
package cfg
import (
"encoding/json"
"io/ioutil"
)
// Load gets your config from the json file,
// and fills your struct with the option
func Load(filename string, o interface{}) error {
b, err := ioutil.ReadFile(filename)
if err == nil {
err = json.Unmarshal(b, &o)
}
return err
}
// Save will save your struct to the given filename,
// this is a good way to create a json template
func Save(filename string, o interface{}) error {
j, err := json.MarshalIndent(&o, "", "\t")
if err == nil {
err = ioutil.WriteFile(filename, j, 0660)
}
return err
}