title: golang nested struct and map
body: <p>I am new to golang and trying to map database query result to my struct which is nested but I am getting errors and not able to figure out the best way to do it. This program is suppose to out JSON - book details group by categories.</p>
  
  <p><strong>Struct is as follows</strong></p>
  
  <pre><code>type book struct{
    category string
    books []*bookDetails
  }
  
  type bookDetails struct{
    name  string
    id    uint
    publisher string.
  }
  </code></pre>
  
  <p><strong>Expected JSON output</strong></p>
  
  <pre><code>[
    {
      "category" : "Ficton",
      "books" : [
                 {name:'aaa',id:12,publisher:'one'},
                 {name:'bbb',id:13,publisher:'two'}
               ]
    },
   {
      "category" : "suspense",
      "books" : [
                 {name:'ccc',id:14,publisher:'three'},
                 {name:'ddd',id:15,publisher:'four'}
               ]
    }
  ]
  </code></pre>
  
  <p>I am not writing SQL query here but please assume similar kind of database schema. <strong>dbRow</strong> in following code is output of select query. Following code is working fine but I am getting all the books in one array, and I am <strong>not able to figure out how can I make it group by categories</strong>.</p>
  
  <pre><code>detailsMap := make(map[uint][]*details)
  for dbRow.Next() {
    var det details
    detErr := dbRow.StructScan(&amp;det)
    detailsMap[det.id] = append(detailsMap[det.id], &amp;det)
  }
  </code></pre>