amuz.es/src/go/misc@v1.0.1/sqls/helper.go (about)

     1  package sqls
     2  
     3  import "github.com/jmoiron/sqlx"
     4  
     5  // MapRows is a convert function,that converts sqlx.Row to specified interface.
     6  func MapRows[T any](rows *sqlx.Rows, itemGetter func() T) (items []T, err error) {
     7  	defer func() { _ = rows.Close() }()
     8  	for rows.Next() {
     9  		item := itemGetter()
    10  		if err = rows.StructScan(item); err != nil {
    11  			return
    12  		}
    13  		items = append(items, item)
    14  	}
    15  
    16  	err = rows.Err()
    17  	return
    18  }