title: Golang repostiory pattern
body: <p>I try to implement repository pattern in Go app (simple web service) and try to find better way to escape code duplication.</p>
  
  <p>Here is a code</p>
  
  <h3>Interfaces are:</h3>
  
  <pre><code>type IRoleRepository interface {
      GetAll() ([]Role, error)
  }
  
  type ISaleChannelRepository interface {
      GetAll() ([]SaleChannel, error)
  }
  </code></pre>
  
  <h3>And implementation:</h3>
  
  <pre><code>func (r *RoleRepository) GetAll() ([]Role, error) {
          var result []Role
          var err error
          var rows *sql.Rows
  
          if err != nil {
              return result, err
          }
  
          connection := r.provider.GetConnection()
          defer connection.Close()
  
          rows, err = connection.Query("SELECT Id,Name FROM Position")
          defer rows.Close()
  
          if err != nil {
              return result, err
          }
  
          for rows.Next() {
              entity := new(Role)
              err = sqlstruct.Scan(entity, rows)
  
              if err != nil {
                  return result, err
              }
  
              result = append(result, *entity)
          }
          err = rows.Err()
          if err != nil {
              return result, err
          }
  
          return result, err
      }
  
      func (r *SaleChannelRepository) GetAll() ([]SaleChannel, error) {
          var result []SaleChannel
          var err error
          var rows *sql.Rows
  
          if err != nil {
              return result, err
          }
  
          connection := r.provider.GetConnection()
          defer connection.Close()
  
          rows, err = connection.Query("SELECT DISTINCT SaleChannel 'Name' FROM Employee")
          defer rows.Close()
  
          if err != nil {
              return result, err
          }
  
          for rows.Next() {
              entity := new(SaleChannel)
              err = sqlstruct.Scan(entity, rows)
  
              if err != nil {
                  return result, err
              }
  
              result = append(result, *entity)
          }
          err = rows.Err()
          if err != nil {
              return result, err
          }
  
          return result, err
      }
  </code></pre>
  
  <p>As you can see differences are in a few words. I try to find something like Generics from C#, but didnt find.</p>
  
  <p>Can anyone help me?</p>