package main import ( "bytes" "encoding/json" "fmt" "log" "os" "regexp" "time" ) const configFileSizeLimit = 10 << 20 var defaultConfig = &struct { netTimeout int64 fileDeadtime string }{ netTimeout: 15, fileDeadtime: "24h", } //有了`json:network`这种注释,后面json解析就可以把相应的数据塞到对应的结构里面来 type Config struct { Network NetworkConfig `json:network` Files []FileConfig `json:files` } type NetworkConfig struct { Servers []string `json:servers` SSLCertificate string `json:"ssl certificate"` SSLKey string `json:"ssl key"` SSLCA string `json:"ssl ca"` Timeout int64 `json:timeout` timeout time.Duration } type FileConfig struct { Paths []string `json:paths` Fields map[string]string `json:fields` DeadTime string `json:"dead time"` deadtime time.Duration } func main() { LoadConfig("E:/golang/src/github.com/test/logstast_forwarder.conf") } func LoadConfig(path string) (config Config, err error) { config_file, err := os.Open(path) if err != nil { emit("Failed to open config file '%s': %s\n", path, err) return } fi, _ := config_file.Stat() if size := fi.Size(); size > (configFileSizeLimit) { emit("config file (%q) size exceeds reasonable limit (%d) - aborting", path, size) return // REVU: shouldn't this return an error, then? } if fi.Size() == 0 { emit("config file (%q) is empty, skipping", path) return } buffer := make([]byte, fi.Size()) _, err = config_file.Read(buffer) emit("\n %s\n", buffer) buffer, err = StripComments(buffer) //去掉注释 if err != nil { emit("Failed to strip comments from json: %s\n", err) return } buffer = []byte(os.ExpandEnv(string(buffer))) //特殊 err = json.Unmarshal(buffer, &config) //解析json格式数据 if err != nil { emit("Failed unmarshalling json: %s\n", err) return } fmt.Printf("111111111 %s \n", config.Network.Servers) for k, _ := range config.Files { if config.Files[k].DeadTime == "" { config.Files[k].DeadTime = defaultConfig.fileDeadtime } config.Files[k].deadtime, err = time.ParseDuration(config.Files[k].DeadTime) if err != nil { emit("Failed to parse dead time duration '%s'. Error was: %s\n", config.Files[k].DeadTime, err) return } } return } func StripComments(data []byte) ([]byte, error) { data = bytes.Replace(data, []byte("\r"), []byte(""), 0) // Windows lines := bytes.Split(data, []byte("\n")) //split to muli lines filtered := make([][]byte, 0) for _, line := range lines { match, err := regexp.Match(`^\s*#`, line) if err != nil { return nil, err } if !match { filtered = append(filtered, line) } } return bytes.Join(filtered, []byte("\n")), nil } func emit(msgfmt string, args ...interface{}) { log.Printf(msgfmt, args...) }
json配置文件:
## json format file { "network": { "servers": [ "localhost:5043" ] }, "files": [ { "paths": [""] } ] }
输出结果:
111111111 [localhost:5043] 2015/10/05 16:45:21 ## json format file { "network": { "servers": [ "localhost:5043" ] }, "files": [ { "paths": [""] } ] }
可见,golang对json文件的解析非常简单和舒服
有疑问加站长微信联系(非本文作者)