因此docs here状态专门使用iterator.Done:Next loads the next row into dst. Its return value is iterator.Done if there are no more results. Once Next returns iterator.Done, all subsequent calls will return iterator.Done.但是,如果我尝试使用“完成”,则会生成编译器错误。实际上,在RowIterator文档here上未定义“完成”。
我的代码(与文档几乎相同):

    it, err := job.Read(ctx)
    if err != nil {
        fmt.Println(err)
    }

    for {
        var rec MyType
        err := it.Next(&rec)
        // the docs say to use Done, but it provides an error

        if err == it.Done {
            break
        }
        if err != nil {
            fmt.Println(err)
        }
        rows = append(rows, rec)
    }
当我尝试构建它时,我得到:./test.go:94:15: it.Done undefined (type *"cloud.google.com/go/bigquery".RowIterator has no field or method Done)我想念什么?

最佳答案

iterator.Done是在迭代器包中定义的variable。因此,将it.Done替换为iterator.Done。这在this example中显示:

package main

import (
    "cloud.google.com/go/bigquery"
    "context"
    "fmt"
    "google.golang.org/api/iterator"
)

func main() {
    ctx := context.Background()
    client, err := bigquery.NewClient(ctx, "project-id")
    if err != nil {
        // TODO: Handle error.
    }
    q := client.Query("select name, num from t1")
    it, err := q.Read(ctx)
    if err != nil {
        // TODO: Handle error.
    }
    for {
        var row []bigquery.Value
        err := it.Next(&row)
        if err == iterator.Done {
            break
        }
        if err != nil {
            // TODO: Handle error.
        }
        fmt.Println(row)
    }
}

关于go - golang bigquery文档指定完成操作符,但生成编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64975153/

10-12 19:32