json文件
{ "filepath":"/home/lee/applications/go/src/github.com/wd-project/src/github.com/conf/conf/fluentd.conf", "serverurl":"http://loki.hy01.internal.wandoujia.com/server/api/servers?type=recursive&node_id=4185", "filetemplate":"/home/lee/applications/go/src/github.com/wd-project/src/github.com/conf/conf/template.conf", "redisconnectmethod":"tcp", "redisaddressport":"127.0.0.1:6379" }
解析json文件
package conf import ( "encoding/json" "fmt" "io/ioutil" "os" ) type GolablConf struct { FilePath string `json:filepath` ServerUrl string `json:serverurl` FileTemplate string `json:filetemplate` RedisConnectMethod string `json:redisconnectmethod` RedisAddressPort string `json:redisaddressport` } var ( config *GolablConf ) func Config() *GolablConf { return config } func ParseConfig(filename string) { f, err := os.Open(filename) if err != nil { fmt.Println("os.Open err=", err) } data, err := ioutil.ReadAll(f) if err != nil { fmt.Println("ioutil.ReadAll err=", err) } var cfg GolablConf err = json.Unmarshal(data, &cfg) if err != nil { fmt.Println("json.Unmarshal err=", err) } config = &cfg }
需要使用json字段,利用conf包,如:
conf.Config().FilePath
就可以使用了但是json字段中有下划线就不能得到对应的字段