I am trying to fetch a string result into a Go struct. I am following the instructions mentioned here under `Example (struct)`: https://pkg.go.dev/cloud.google.com/go/bigquery#RowIterator.Next
Following is the simplified repro of what I am trying to do.
type results struct {
stack string
}
func read(ctx context.Context, client *bigquery.Client) error {
query := `
SELECT
stack as stack
FROM tab1
limit 3;`
q := client.Query(query)
it, err := q.Read(ctx)
if err != nil {
fmt.Printf("%v", err)
return err
}
for {
var row results
err := it.Next(&row)
if err == iterator.Done {
break
}
if err != nil {
return err
}
fmt.Printf("stack: %v", row.stack)
}
return nil
}
I can put a breakpoint and see that results are fetched correctly from the table but aren't getting assigned to the struct field as they should be. I am not sure if the example in that link is incorrect or if I am making some silly mistake. Can someone please help?
Thank you
Solved! Go to Solution.
It was a silly mistake after all. The struct field name was not exported, i.e. did not begin with a capital letter. Following is the correct way.
type results struct {
Stack string
}
It was a silly mistake after all. The struct field name was not exported, i.e. did not begin with a capital letter. Following is the correct way.
type results struct {
Stack string
}