goconf + yaml 配置文件

1.不使用某个字段要注释掉该字段,而不是

at_userid  (空格,没有值) 这样

 

2.时间参数,可以是100s这样,自动转换了

 

3.列表

KafkaAddrs    []string                    `goconf:"kafka:addrs:,"`

配置文件      topics  KafkaTopic1,KafkaTopic2

 

yml 

配置文件:

server:    

    etcd_cluster_list:

        etcd_cluster: [127.0.0.1:2379, 192.168.0.1:2377]    

        etcd_cluster: [127.0.0.1:2378, 127.0.0.1:2377]       

    addr: 0.0.0.0:6580

代码结构:

"gopkg.in/yaml.v2"

type EtcdClusterList struct {
	EtcdCluster []string  `yaml:"etcd_cluster"`
}

type SrvWebConfig struct {
	//EtcdCluster []string `yaml:"etcd_cluster"` //Endpoints:[]string{"118.89.177.83:2379"}, 
	EtcdList []EtcdClusterList `yaml:"etcd_cluster_list,flow"`
	AppMode string `yaml:"app_mode"`
	Addr string `yaml:"addr"`	
}

type Config struct {
	SrvWebCfg SrvWebConfig `yaml:"server"`
}



var config = &Config{}

func ReloadConfig(confFile string) (err error) {
	f, err := os.Open(confFile)
	if err != nil {
		log.WithError(err).Fatal("Open config failed.")
		return err
	}

	defer f.Close()
	if err = yaml.NewDecoder(f).Decode(config); err != nil {
		log.WithError(err).Fatal("Decode config failed.")
		return  err
	}
	log.Info("ReloadConfig success ")
	return nil
}

 

 

 

你可能感兴趣的:(golang)