refactor(scan.go): array index is out of bounds exits the for loop

This commit is contained in:
demoManito 2024-03-07 16:27:08 +08:00
parent f7ebf049da
commit 4c7c8f596d

View File

@ -311,13 +311,16 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
db.scanIntoStruct(rows, elem, values, fields, joinFields)
if !update {
// array index is out of bounds, exits the for loop
if isArrayKind && reflectValue.Len() < int(db.RowsAffected) {
break
}
if !isPtr {
elem = elem.Elem()
}
if isArrayKind {
if reflectValue.Len() >= int(db.RowsAffected) {
reflectValue.Index(int(db.RowsAffected - 1)).Set(elem)
}
reflectValue.Index(int(db.RowsAffected - 1)).Set(elem)
} else {
reflectValue = reflect.Append(reflectValue, elem)
}