1 回答

TA貢獻1806條經驗 獲得超5個贊
從一個 nil(內部)切片和一個 nil 切片開始。當您將新記錄附加到內部切片時。當所有者 ID 更改時,將內部切片附加到切片切片的末尾并將其設置為 nil 以開始構建另一個內部切片。
請注意,您不需要保留計數cntProp,contadorOwners因為您只需附加切片。此外,如果您從nil切片開始,您可以追加,并且將為您分配底層數組。這是一些接近可能對您有用但我沒有測試過的代碼:
rows, err := dbLeasity.Query(contractsQuery)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
current_owner_id := int64(-1)
var grantJointResponse [][]granJoin
var oneSlice []granJoin
for rows.Next() {
var auxJoin granJoin
if err := rows.Scan(&auxJoin.owner_id, &auxJoin.property_id); err != nil {
log.Fatal(err)
}
if (auxJoin.owner_id.Valid){
if (auxJoin.owner_id.Int64 != current_owner_id){
if oneSlice != nil {
grantJointResponse = append(grantJointResponse, oneSlice)
oneSlice = nil
}
current_owner_id = auxJoin.owner_id.Int64
}
oneSlice = append(oneSlice, auxJoin)
}
}
if oneSlice != nil {
grantJointResponse = append(grantJointResponse, oneSlice)
}
- 1 回答
- 0 關注
- 131 瀏覽
添加回答
舉報