go
/
misc
1
0
Fork 0
misc/sqls/helper.go

19 lines
413 B
Go
Raw Permalink Normal View History

2022-03-21 22:13:57 +09:00
package sqls
import "github.com/jmoiron/sqlx"
// MapRows is a convert function,that converts sqlx.Row to specified interface.
func MapRows[T any](rows *sqlx.Rows, itemGetter func() T) (items []T, err error) {
defer func() { _ = rows.Close() }()
for rows.Next() {
item := itemGetter()
if err = rows.StructScan(item); err != nil {
return
}
items = append(items, item)
}
err = rows.Err()
return
}