douwulu2576 2016-08-15 13:01
浏览 74
已采纳

使用Golang Viper读取地图片段

I'm using the excellent viper library from here: https://github.com/spf13/viper

I'm trying to read in a config file in hcl (although it could be a JSOn or YAML file as well) which looks like this:

interval = 10
statsd_prefix = "pinger"



group "dns" {
  target_prefix = "ping"
  target "dns" {
    hosts = [
      "dnsserver1",
      "dnsserver2"
    ]
  }
}

The code I have so far looks like this:

viper.SetConfigName("config")
viper.AddConfigPath(".")

err := viper.ReadInConfig()

if err != nil {
  panic(fmt.Errorf("Fatal error config file: %s 
", err))
}

interval := viper.GetInt("interval")
prefix := viper.GetString("statsd_prefix")

groups := viper.GetStringMap("group")

fmt.Println(interval)
fmt.Println(prefix)

The big problem I'm having is with the group option. This can be multiple different groups.

It doesn't seem to work when I read it in using viper.GetStringMap, so I used the standard viper.Get function. The resulting structure looks like this when dumped:

([]map[string]interface {}) (len=1 cap=1) {
 (map[string]interface {}) (len=1) {
  (string) (len=3) "dns": ([]map[string]interface {}) (len=1 cap=2) {
   (map[string]interface {}) (len=2) {
    (string) (len=13) "target_prefix": (string) (len=4) "ping",
    (string) (len=6) "target": ([]map[string]interface {}) (len=1 cap=1) {
     (map[string]interface {}) (len=1) {
      (string) (len=8) "dns": ([]map[string]interface {}) (len=1 cap=1) {
       (map[string]interface {}) (len=1) {
        (string) (len=5) "hosts": ([]interface {}) (len=2 cap=2) {
         (string) (len=18) "dnsserver1",
         (string) (len=18) "dnsserver2"
        }
       }
      }
     }
    }
   }
  }
 }
}

It seems to be of type slice when I use reflect. Do I need to cast it to a slice? How do I do that? Is there an easier way of managing a data structure like this?

I'm completely new to golang, so please go easy on me :)

  • 写回答

1条回答 默认 最新

  • doudou3935 2016-08-15 15:26
    关注

    Instead of call raw Get and then decide what exactly you get I'd suggest first describe your desired config structure, something like

    type config struct {
        interval int `mapstructure:"Interval"`
        statsdPrefix string `mapstructure:"statsd_prefix"`
        groups []group
    }
    type group struct {
        group string `mapstructure:"group"`
        targetPrefix string `mapstructure:"target_prefix"`
        targets []target
    }
    type target struct {
        target string `mapstructure:"target"`
        hosts []string `mapstructure:"hosts"`
    }
    

    and than unmarshall(decode) in it

    var C config
    
    err := viper.Unmarshal(&C)
    if err != nil {
        t.Fatalf("unable to decode into struct, %v", err)
    }
    

    assuming decoder is smart enough to unmurshall in provided structure if it possible and meaningful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。