title: In Go is there a way to convert map of structure to slice of structure
body: <p>I have to convert map of structure to slice of structure in Golang, i.e. source to target structure specified below.</p>
  
  <pre><code>// Source
  var source map[string]Category
  
  type Category struct {
      A           int
      SubCategory map[string]SubCategory
  }
  
  type SubCategory struct {
      B int
      C string
  }
  
  </code></pre>
  
  <pre><code>// Target
  var target []OldCategory
  
  type OldCategory struct {
      OldA           int `mapstructure:"A"`
      OldSubCategory []OldSubCategory
  }
  
  type OldSubCategory struct {
      OldB int    `mapstructure:"B"`
      OldC string `mapstructure:"C"`
  }
  </code></pre>
  
  <p>I am referring mapstrucuture package ("github.com/mitchellh/mapstructure").
  One way to convert from source to target is to iterate on all the SubCategory and then Category in source instance and use mapstructure.Decode() to convert each one individually.</p>
  
  <p>Is there a direct way using mapstrucuture package, wherein I create a custom decoder hook using NewDecoder and DecoderConfig.DecodeHook, and whenever I encounter source as a map of structure and target as a slice of structure, I handle it in the DecodeHookFunc function.</p>
  
  <p>Relevant documentation of mapstructure
  <a href="https://godoc.org/github.com/mitchellh/mapstructure#NewDecoder" rel="nofollow noreferrer">https://godoc.org/github.com/mitchellh/mapstructure#NewDecoder</a></p>