toml.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/pflag"
  5. "github.com/spf13/viper"
  6. "log"
  7. )
  8. //
  9. //type songInfo struct {
  10. // Name string
  11. // Duration int
  12. //}
  13. //
  14. //type dataBase struct {
  15. // Server string
  16. // Ports []int
  17. //}
  18. //
  19. //type servers struct {
  20. // Alpha server //注意Alpha等首字母大写
  21. // Beta server
  22. //}
  23. //
  24. //type server struct {
  25. // Ip string
  26. // Dc string
  27. //}
  28. //
  29. //type clients struct {
  30. // Data [][]interface{}
  31. //}
  32. //
  33. //type config struct {
  34. // Bc string
  35. // Song songInfo
  36. // Database dataBase
  37. // Servers servers
  38. // Clients clients
  39. //}
  40. //
  41. ////单例模式
  42. //var (
  43. // cfg *config
  44. // once sync.Once
  45. //)
  46. //
  47. //func Config() *config {
  48. // once.Do(func() {
  49. // filePath, err := filepath.Abs("./ch3/config.toml")
  50. // if err != nil {
  51. // panic(err)
  52. // }
  53. // fmt.Printf("parse toml file once. filePath: %s\n", filePath)
  54. // if _, err := toml.DecodeFile(filePath, &cfg); err != nil {
  55. // panic(err)
  56. // }
  57. // })
  58. // return cfg
  59. //}
  60. //绑定命令行
  61. func init() {
  62. pflag.String("cmd", "cmd", "")
  63. viper.BindPFlags(pflag.CommandLine)
  64. }
  65. /*
  66. 调用Set显示设置的;
  67. 命令行选项;
  68. 环境变量;
  69. 配置文件;
  70. 默认值。
  71. */
  72. func main() {
  73. pflag.Parse()
  74. //var cg config
  75. //cpath := "./example.toml"
  76. //if _,err := toml.DecodeFile(cpath,&cg);err != nil {
  77. // log.Fatal(err)
  78. //}
  79. //fmt.Printf("%v %v\n",cg.Bc,cg.Song.Name)
  80. //fmt.Printf("%v %v\n",cg.Database.Server,cg.Database.Ports[2])
  81. //fmt.Printf("%v %v\n",cg.Servers.Alpha.Dc,cg.Servers.Beta.Ip)
  82. //fmt.Printf("%v %v\n",cg.Clients.Data[0][0],cg.Clients.Data[1][0])
  83. viper.SetConfigName("example")
  84. viper.SetConfigType("toml")
  85. viper.AddConfigPath(".")
  86. if err := viper.ReadInConfig(); err != nil {
  87. log.Fatalf("read config failed: %v", err)
  88. }
  89. fmt.Println(viper.Get("Bc"))
  90. fmt.Println(viper.Get("song.Name"))
  91. ports := viper.GetIntSlice("database.ports")
  92. fmt.Println(ports[2])
  93. fmt.Println("port", viper.GetIntSlice("database.ports")[0])
  94. //servers:= viper.GetStringMapString()
  95. fmt.Println("servers.alpha.ip", viper.GetString("servers.alpha.ip"))
  96. clients := viper.GetStringMap("clients")
  97. for _, v := range clients {
  98. fmt.Println(v)
  99. for _, j := range v.([]interface{}) {
  100. fmt.Println(j)
  101. for _, k := range j.([]interface{}) {
  102. fmt.Println(k)
  103. }
  104. }
  105. }
  106. fmt.Println("all settings: ", viper.AllSettings())
  107. //绑定环境变量
  108. viper.AutomaticEnv()
  109. fmt.Println("GOPATH: ", viper.Get("GOPATH"))
  110. fmt.Println("cmd-line: ", viper.Get("cmd"))
  111. viper.SetDefault("appversion", "0.0.1")
  112. fmt.Println("appversion", viper.Get("appversion"))
  113. }