如何在Golang中声明没有集合len或容量的结构切片

如何在Golang中声明没有集合len或容量的结构切片,go,data-structures,slice,Go,Data Structures,Slice,我试图用我的结构的切片填充grantJointResponse,它来自一个查询的数据,切片可以有不同的大小。这在Python或JS等语言中很容易实现,但我在Golang中尝试了几种组合,但无法实现。我试过使用grantJointResponse[contadorowns]=make(granJoin,0),使用grantJointResponse:=[[]]granJoin{},使用grantJointResponse[contadorowns]=granJoin{},我想不出来,很可能这是件容

我试图用我的结构的切片填充grantJointResponse,它来自一个查询的数据,切片可以有不同的大小。这在Python或JS等语言中很容易实现,但我在Golang中尝试了几种组合,但无法实现。我试过使用grantJointResponse[contadorowns]=make(granJoin,0),使用grantJointResponse:=[[]]granJoin{},使用grantJointResponse[contadorowns]=granJoin{},我想不出来,很可能这是件容易的事,但我没有看到(我对Golang有点陌生)。此当前版本在grantJointResponse[contadorOwners]=[]granJoin{auxJoin}中获取的索引超出范围。因此,如果有人知道如何做到这一点,我们将不胜感激:)

我希望创造这样的东西:

// Data that will be in the rows
granJoin1 = { {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}
}
从零(内部)切片和零切片开始。当您获得新记录时,会将其附加到内部切片。当所有者ID更改时,将内部切片附加到切片末尾,并将其设置为nil以开始构建另一个内部切片

请注意,您不需要保留counts
cntProp
contadowners
,因为您只需要附加切片。此外,如果您从
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)
}

完成围棋之旅的各个部分。忽略
go-vet
警告。并且忽略这样一个事实:代码似乎对已经排序的片段进行了不必要的排序(该片段仅按偶然情况排序)。实际上,初始结果不会被排序,因为Go中的贴图是故意未排序的,即,在未排序的贴图上循环并将其元素附加到切片将生成未排序的切片。要创建没有设置长度的切片,只需使用“附加”。在您的代码中,不是创建单个元素片段并附加到它,而是从nil片段开始。附加到nil片将创建该片,包括分配内存。
// Data that will be in the rows
granJoin1 = { {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}
}
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)
}