为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 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 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信