广告平台(站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init_cfg.go 1.0 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package cfg
  2. import (
  3. "flag"
  4. "io/ioutil"
  5. "gopkg.in/yaml.v2"
  6. )
  7. // 配置文件数据,全局变量
  8. var (
  9. Debug bool
  10. Prd bool
  11. CurlDebug bool
  12. SrvAddr string
  13. RedisAddr string
  14. MQ *MQCfg
  15. DB *DBCfg
  16. Log *LogCfg
  17. Local bool
  18. )
  19. // 初始化配置文件,将cfg.yml读入到内存
  20. func InitCfg() {
  21. //用指定的名称、默认值、使用信息注册一个string类型flag。
  22. path := flag.String("c", "etc/cfg.yml", "config file")
  23. //解析命令行参数写入注册的flag里。
  24. //解析之后,flag的值可以直接使用。
  25. flag.Parse()
  26. var (
  27. c []byte
  28. err error
  29. conf *Config
  30. )
  31. if c, err = ioutil.ReadFile(*path); err != nil {
  32. panic(err)
  33. }
  34. //yaml.Unmarshal反序列化映射到Config
  35. if err = yaml.Unmarshal(c, &conf); err != nil {
  36. panic(err)
  37. }
  38. //数据读入内存
  39. Prd = conf.Prd
  40. Debug = conf.Debug
  41. Local = conf.Local
  42. CurlDebug = conf.CurlDebug
  43. DB = &conf.DB
  44. Log = &conf.Log
  45. RedisAddr = conf.RedisAddr
  46. SrvAddr = conf.SrvAddr
  47. MQ = &conf.MQ
  48. }