小编Fan*_*wan的帖子

将嵌套配置 Yaml 映射到结构

我是新手,我正在使用 viper 加载我所有的配置,目前我拥有的是 YAML,如下所示

 countryQueries:
  sg:
    - qtype: gmap
      qplacetype: postal_code
    - qtype: gmap
      qplacetype: address
    - qtype: geocode
      qplacetype: street_address

  hk:
    - qtype: gmap
      qplacetype: postal_code
    - qtype: gmap
      qplacetype: address
    - qtype: geocode
      qplacetype: street_address
Run Code Online (Sandbox Code Playgroud)

请注意,国家/地区代码是动态的,可以随时为任何国家/地区添加。那么我如何将它映射到一个从技术上讲我可以做的结构

for _, query := range countryQueries["sg"] { }
Run Code Online (Sandbox Code Playgroud)

我尝试通过循环来自己构建它,但我卡在这里

for country, queries := range viper.GetStringMap("countryQueries") {
    // i cant seem to do anything with queries, which i wish to loop it
    for _,query := range queries {} //here error
}
Run Code Online (Sandbox Code Playgroud)

任何帮助都感激不尽

struct config viper go

2
推荐指数
1
解决办法
4815
查看次数

标签 统计

config ×1

go ×1

struct ×1

viper ×1