Go - Generic
For use with the official BigQuery Golang client
Golang does not support a direct environment variable override for the host like Python/Node. However we show here a simple snippet that can be adapted to your needs, giving the exact same functionality with the same semantics. We recommend , if you are not already doing it as a best practice, that your BigQuery client initialisation code is kept in one place and referenced throughout your codebase.
As with Python and Node, ensure that the environment variable is set as such:
BIGQUERY_EMULATOR_HOST=https://<your proxy>.alvin.ai
Here is the code snippet:
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