List taxonomies

List existing taxonomies.

Code sample

Go

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

import (
	"context"
	"fmt"
	"io"

	datacatalog "cloud.google.com/go/datacatalog/apiv1beta1"
	"google.golang.org/api/iterator"
	datacatalogpb "google.golang.org/genproto/googleapis/cloud/datacatalog/v1beta1"
)

// listTaxonomies prints information about the taxonomies contained within a specific
// project and location.
func listTaxonomies(w io.Writer, projectID, location string) error {
	// projectID := "my-project-id"
	// location := "us"
	ctx := context.Background()
	policyClient, err := datacatalog.NewPolicyTagManagerClient(ctx)
	if err != nil {
		return fmt.Errorf("datacatalog.NewPolicyTagManagerClient: %v", err)
	}
	defer policyClient.Close()

	req := &datacatalogpb.ListTaxonomiesRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
	}
	it := policyClient.ListTaxonomies(ctx, req)
	fmt.Fprintf(w, "listing taxonomies in project %s and location %s\n", projectID, location)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListTaxonomies iteration error: %v", err)
		}

		fmt.Fprintf(w, "\t- %s: %s\n", resp.Name, resp.DisplayName)
	}
	return nil
}

What's next

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