set the service endpoint

This sample demonstrates how to set an alternate endpoint when using the BigQuery client libraries.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Go API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

import (
	"context"
	"fmt"

	"cloud.google.com/go/bigquery"
	"google.golang.org/api/option"
)

// setClientEndpoint creates a bigquery.Client pointing
// to a specific endpoint.
func setClientEndpoint(endpoint, projectID string) (*bigquery.Client, error) {
	// projectID := "my-project-id"
	// endpoint := "https://bigquery.googleapis.com/bigquery/v2/
	ctx := context.Background()
	client, err := bigquery.NewClient(ctx, projectID, option.WithEndpoint(endpoint))
	if err != nil {
		return nil, fmt.Errorf("bigquery.NewClient: %w", err)
	}
	defer client.Close()

	return client, nil
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.