From 118dc07e10c6ac4980dd81f71d280f39749747a7 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 1 Jun 2022 15:53:08 +0800 Subject: [PATCH] service: fix reflect rows scan --- pkg/service/reflect.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/service/reflect.go b/pkg/service/reflect.go index a20aebb7b..86d46ca91 100644 --- a/pkg/service/reflect.go +++ b/pkg/service/reflect.go @@ -165,15 +165,16 @@ func scanRowsOfType(rows *sqlx.Rows, tpe interface{}) (interface{}, error) { refType = refType.Elem() } - sliceRef := reflect.New(reflect.SliceOf(refType)) + sliceRef := reflect.MakeSlice(reflect.SliceOf(refType), 0, 100) + // sliceRef := reflect.New(reflect.SliceOf(refType)) for rows.Next() { var recordRef = reflect.New(refType) var record = recordRef.Interface() - if err := rows.StructScan(&record); err != nil { + if err := rows.StructScan(record); err != nil { return sliceRef.Interface(), err } - sliceRef = reflect.Append(sliceRef, recordRef) + sliceRef = reflect.Append(sliceRef, recordRef.Elem()) } return sliceRef.Interface(), rows.Err() @@ -200,4 +201,3 @@ func selectAndScanType(ctx context.Context, db *sqlx.DB, sel squirrel.SelectBuil defer rows.Close() return scanRowsOfType(rows, tpe) } -