Search code examples
dictionarygogoogle-bigquerylabelgo-map

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


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:

Screenshot of code containing line numbers

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


Solution

  • 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