title: Retrieve relation one to many into JSON sql pure, Golang, Performance
body: <p>Suppose that I've the following structures that it's  the mapped tables.</p>
  
  <pre class="lang-golang prettyprint-override"><code>type Publisher struct{
     ID int       `db:"id"`
     Name  string `db:"name"`
     Books []*Book  
  }
  
  type Book struct {
     ID int       `db:"id"`
     Name string  `db:"name"`
     PublisherID   `db:"publisher_id"` 
  }
  </code></pre>
  
  <p>So, What if I wanna retrieve all the Publisher  with all related Books I would like to get a <code>JSON</code> like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>[ //Publisher 1 
    {
      "id" : "10001", 
      "name":"Publisher1",
      "books" : [
         { "id":321,"name": "Book1"}, 
         { "id":333,"name": "Book2"}
      ]
    },
    //Publisher 2
    {
      "id" : "10002", 
      "name":"Slytherin Publisher",
      "books" : [
         { "id":4021,"name": "Harry Potter and the Chamber of Secrets"}, 
         { "id":433,"name": "Harry Potter and the Order of the Phoenix"}
      ]
    },
  ]
  </code></pre>
  
  <p>So I've the following structure that I use to retrieve all kind of query related with <code>Publisher</code></p>
  
  <pre class="lang-golang prettyprint-override"><code>type PublisherRepository struct{
     Connection *sql.DB
  }
  // GetEbooks return all the books related with a publisher
  func (r *PublisherRepository) GetBooks(idPublisher int) []*Book {
      bs := make([]Book,0)
      sql := "SELECT * FROM books b WHERE b.publisher_id =$1 "
      row, err := r.Connection.Query(sql,idPublisher)
      if err != nil {
        //log
      }
      for rows.Next() {
        b := &amp;Book{}
        rows.Scan(&amp;b.ID, &amp;b.Name, &amp;b.PublisherID)
        bs := append(bs,b)
      }
      return bs
  }
  func (r *PublisherRepository) GetAllPublishers() []*Publisher {
      sql := "SELECT * FROM publishers"
      ps := make([]Publisher,0)
      rows, err := r.Connection.Query(sql)
      if err != nil { 
         // log 
      }
      for rows.Next() {
         p := &amp;Publisher{}
         rows.Scan(&amp;p.ID,&amp;p.Name)
         // Is this the best way? 
         books := r.GetBooks(p.ID)
         p.Books  = books
      }
      return ps
  
  }
  </code></pre>
  
  <p>So , here my questions</p>
  
  <ol>
  <li><p>What is the best approach to retrieve all the <code>publisher</code> with the best performance, because a <code>for</code> inside a <code>for</code> is not the best solution, what if I've 200 publisher and in the average of each publisher has 100 books.</p></li>
  <li><p>Is  in GoLang idiomatic <code>PublisherRepository</code> or is there another way to create something to manage the transactions of an entity with pure sql?</p></li>
  </ol>