Go - Generic
For use with the official BigQuery Golang client
package main
import (
"context"
"fmt"
"log"
"os"
"cloud.google.com/go/bigquery"
"google.golang.org/api/option"
)
func main() {
// Set emulator host (can also be set via env variable)
endpoint := os.Getenv("BIGQUERY_EMULATOR_HOST")
if endpoint == "" {
endpoint = "https://bigquery.googleapis.com"
}
endpoint = endpoint + "/bigquery/v2/"
ctx := context.Background()
client, err := bigquery.NewClient(ctx, "bigquery-public-data", option.WithEndpoint(endpoint))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
fmt.Println("client:", client)
// List datasets
it := client.Datasets(ctx)
for {
ds, err := it.Next()
if err != nil {
fmt.Println("error: ", err)
break
}
fmt.Println("Dataset:", ds.DatasetID)
}
defer func(client *bigquery.Client) {
err := client.Close()
if err != nil {
log.Println("Failed to close client: %v", err)
}
}(client)
}Last updated