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.

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

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格