package cfg import ( "flag" "io/ioutil" "gopkg.in/yaml.v2" ) //配置文件数据,全局变量 var ( Debug bool Prd bool CurlDebug bool SrvAddr string RedisAddr string DB *DBCfg DataDB *DBCfg MQ *MQCfg ES *ESCfg Log *LogCfg ArkID *ArkIDCfg Admin *AdminCfg Official *OfficialCfg WxappletFilepath *WxappletFilepathCfg Local bool AppComm *AppCommCfg MqWorkQueueName string RabbitMqAddr string ) //初始化配置文件,将cfg.yml读入到内存 func InitCfg() { //用指定的名称、默认值、使用信息注册一个string类型flag。 path := flag.String("c", "etc/cfg.yml", "config file") //解析命令行参数写入注册的flag里。 //解析之后,flag的值可以直接使用。 flag.Parse() var ( c []byte err error conf *Config ) if c, err = ioutil.ReadFile(*path); err != nil { panic(err) } //yaml.Unmarshal反序列化映射到Config if err = yaml.Unmarshal(c, &conf); err != nil { panic(err) } //数据读入内存 Prd = conf.Prd Debug = conf.Debug Local = conf.Local CurlDebug = conf.CurlDebug DB = &conf.DB DataDB = &conf.DataDB Log = &conf.Log ArkID = &conf.ArkID RedisAddr = conf.RedisAddr SrvAddr = conf.SrvAddr Admin = &conf.Admin Official = &conf.Official WxappletFilepath = &conf.WxappletFilepath AppComm = &conf.AppComm MqWorkQueueName = conf.MqWorkQueueName RabbitMqAddr = conf.RabbitMqAddrTest if conf.Prd { RabbitMqAddr = conf.RabbitMqAddr } MQ = &conf.MQ ES = &conf.ES }