亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Golang 中聲明沒有設置長度和容量的結構切片

如何在 Golang 中聲明沒有設置長度和容量的結構切片

Go
一只名叫tom的貓 2022-06-27 17:18:13
我正在嘗試用我的 struct granJoin切片填充grantJointResponse,它來自查詢的數據,切片可以有不同的大小。這在 Python 或 JS 等語言中很容易,但我在 Golang 中嘗試了幾種組合,但無法使其工作。我的壓力與切片的聲明有關,我嘗試過grantJointResponse[contadorOwners] = make(granJoin, 0),grantJointResponse := [][]granJoin{},grantJointResponse[contadorOwners] = granJoin{}而且我無法弄清楚,很可能這很容易而且我沒有看到它(我對Golang有點陌生)。這個當前版本得到一個grantJointResponse[contadorOwners] = []granJoin{auxJoin} 中的索引超出范圍。因此,如果有人知道如何做到這一點,將不勝感激:)import (    "fmt"    .    .    .    "log"    _ "github.com/lib/pq"     "database/sql")type granJoin struct{    property_id sql.NullInt64    owner_id sql.NullInt64}rows, err := dbLeasity.Query(contractsQuery)if err != nil {    log.Fatal(err)}defer rows.Close()current_owner_id := int64(-1)contadorOwners := -1cntProp := 0var grantJointResponse [][]granJoinfor 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 (current_owner_id == -1){            grantJointResponse = [][]granJoin{{auxJoin}}        }        if (auxJoin.owner_id.Int64 != current_owner_id){            cntProp = 0            contadorOwners++            current_owner_id = auxJoin.owner_id.Int64             if (current_owner_id != -1){                grantJointResponse[contadorOwners] = []granJoin{auxJoin}            }        }        if (cntProp != 0){            grantJointResponse[contadorOwners] = append(grantJointResponse[contadorOwners], auxJoin)        }        cntProp++    }}我希望創造這樣的東西:// Data that will be in the rowsgranJoin1 = { {true, 1}, {true, 10} }granJoin2 = { {true, 2}, {true, 11} }granJoin3 = { {true, 2}, {true, 12} }granJoin4 = { {true, 2}, {true, 13} }granJoin5 = { {true, 3}, {true, 14} }granJoin6 = { {true, 3}, {true, 15} }//The way I need to be on the Slice of Slices grantJointResponse := {  {granJoin1},  {granJoin2, granJoin3, granJoin4},  {granJoin5, granJoin6}}
查看完整描述

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)

}


查看完整回答
反對 回復 2022-06-27
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號