英文:
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查询中添加标签的官方文档/示例吗?
英文:
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:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论