尝试在GoLang中传递标签运行BQ查询,但出现恐慌错误。

huangapple go评论79阅读模式
英文:

Trying to run a query in BQ passing labels in GoLang, but getting panic error

问题

我正在尝试运行以下代码,在GoLang中传递标签来查询BQ,但是遇到了恐慌错误(您可以参考附图中的行号)。我对Golang还不熟悉,有人可以指导一下吗?

query := client.Query(`select * from dataset.table`)
fmt.Println(query.Labels)

query.Labels["test-key"] = "test-value"

output, err := query.Read(ctx)

但是出现以下错误:

map[]
panic: assignment to entry in nil map

goroutine 1 [running]:
main.query({0x17fc6e8, 0xc000122000}, 0xc00003e135?)
        test_bq.go:54 +0xd6
main.main()
        test_bq.go:30 +0x167

带有行号的代码:

尝试在GoLang中传递标签运行BQ查询,但出现恐慌错误。

请问有人可以将我重定向到有关如何通过golang在BQ查询中添加标签的官方文档/示例吗?

英文:

I am trying to run the following code to query in BQ passing labels in GoLang, but getting panic error (You may refer attached image for linenumbers). I am new to Golang, can someone please guide here?

query := client.Query(`select * from dataset.table`)
fmt.Println(query.Labels)

query.Labels["test-key"] = "test-value"

output, err := query.Read(ctx)

but getting following error :

map[]
panic: assignment to entry in nil map

goroutine 1 [running]:
main.query({0x17fc6e8, 0xc000122000}, 0xc00003e135?)
        test_bq.go:54 +0xd6
main.main()
        test_bq.go:30 +0x167

Code with linenumbers:

尝试在GoLang中传递标签运行BQ查询,但出现恐慌错误。

Can someone please redirect me to official documentation/samples on how to add labels in BQ queries via golang (if there's any)

答案1

得分: 0

我假设问题是在运行client.Query()时,map query.Labels actual没有初始化。只需创建一个map即可解决此问题。请尝试以下代码:

query := client.Query(`select * from dataset.table`)
fmt.Println(query.Labels)

query.Labels = map[string]string{"test-key": "test-value"}

output, err := query.Read(ctx)

return output, err

希望对你有帮助!

英文:

I assume the issue is that the map query.Labels actual is not initiated when running client.Query(). Simply creating a map should fix this. Try this:

query := client.Query(`select * from dataset.table`)
fmt.Println(query.Labels)

query.Labels = map[string]string{"test-key": "test-value"}

output, err := query.Read(ctx)

return output, err

huangapple
  • 本文由 发表于 2022年11月3日 03:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/74294561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定