title: Reading a slice of maps with Golang Viper
body: <p>I'm using the excellent viper library from here: <a href="https://github.com/spf13/viper" rel="noreferrer">https://github.com/spf13/viper</a></p>
  
  <p>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:</p>
  
  <pre><code>interval = 10
  statsd_prefix = "pinger"
  
  
  
  group "dns" {
    target_prefix = "ping"
    target "dns" {
      hosts = [
        "dnsserver1",
        "dnsserver2"
      ]
    }
  }
  </code></pre>
  
  <p>The code I have so far looks like this:</p>
  
  <pre><code>viper.SetConfigName("config")
  viper.AddConfigPath(".")
  
  err := viper.ReadInConfig()
  
  if err != nil {
    panic(fmt.Errorf("Fatal error config file: %s \n", err))
  }
  
  interval := viper.GetInt("interval")
  prefix := viper.GetString("statsd_prefix")
  
  groups := viper.GetStringMap("group")
  
  fmt.Println(interval)
  fmt.Println(prefix)
  </code></pre>
  
  <p>The big problem I'm having is with the group option. This can be multiple different groups.</p>
  
  <p>It doesn't seem to work when I read it in using <code>viper.GetStringMap</code>, so I used the standard <code>viper.Get</code> function. The resulting structure looks like this when dumped:</p>
  
  <pre><code>([]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"
          }
         }
        }
       }
      }
     }
    }
   }
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>I'm completely new to golang, so please go easy on me :)</p>