title: Generalizing *sql.Rows Scan in Go
body: <p>I am developing a web API using Go and there is a lot of redundant database query scan code. </p>
  
  <pre><code>func (m *ContractModel) WorkQuestions(cid int) ([]models.WorkQuestion, error) {
      results, err := m.DB.Query(queries.WORK_QUESTIONS, cid)
      if err != nil {
          return nil, err
      }
  
      var workQuestions []models.WorkQuestion
      for results.Next() {
          var wq models.WorkQuestion
          err = results.Scan(&amp;wq.ContractStateID, &amp;wq.QuestionID, &amp;wq.Question, &amp;wq.ID, &amp;wq.Answer, &amp;wq.Compulsory)
          if err != nil {
              return nil, err
          }
          workQuestions = append(workQuestions, wq)
      }
  
      return workQuestions, nil
  }
  
  func (m *ContractModel) Questions(cid int) ([]models.Question, error) {
      results, err := m.DB.Query(queries.QUESTIONS, cid)
      if err != nil {
          return nil, err
      }
  
      var questions []models.Question
      for results.Next() {
          var q models.Question
          err = results.Scan(&amp;q.Question, &amp;q.Answer)
          if err != nil {
              return nil, err
          }
          questions = append(questions, q)
      }
  
      return questions, nil
  }
  
  func (m *ContractModel) Documents(cid int) ([]models.Document, error) {
      results, err := m.DB.Query(queries.DOCUMENTS, cid)
      if err != nil {
          return nil, err
      }
  
      var documents []models.Document
      for results.Next() {
          var d models.Document
          err = results.Scan(&amp;d.Document, &amp;d.S3Region, &amp;d.S3Bucket, &amp;d.Source)
          if err != nil {
              return nil, err
          }
          documents = append(documents, d)
      }
  
      return documents, nil
  }
  </code></pre>
  
  <p>I need to generalize this code so that I can pass in the result <code>*sql.Rows</code> to a function and obtain a struct slice containing the scanned rows. I know that there is a <code>StructScan</code> method in <strong>sqlx</strong> package but this cannot be used since I have a significant amount of code written using the go standard <strong>database/sql</strong> package. </p>
  
  <p>Using the reflect package, I can create a generic StructScan function but reflect package cannot create a slice of struct from a passed interface{} type. What I need to achieve is something like as follows</p>
  
  <pre><code>func RowsToStructs(rows *sql.Rows, model interface{}) ([]interface{}, error) {
      // 1. Create a slice of structs from the passed struct type of model
      // 2. Loop through each row,
      // 3. Create a struct of passed mode interface{} type
      // 4. Scan the row results to a slice of interface{}
      // 5. Set the field values of struct created in step 3 using the slice in step 4
      // 6. Add the struct created in step 3 to slice created in step 1
      // 7. Return the struct slice
  }
  </code></pre>
  
  <p>I cannot seem to find a way to scan the struct passed as the model parameter and create a slice of it using the reflect package. Is there any workaround to this or am I looking at the question in a wrong way?</p>
  
  <p><em>Struct fields has the correct number of cols returned from the result and in correct order</em></p>