tessl install tessl/golang-cloud-google-com--go--pubsub@1.50.5Google Cloud Pub/Sub client library for Go providing high-level idiomatic APIs for publishing and receiving messages with automatic batching, flow control, and support for advanced features including message ordering, schema validation, exactly-once delivery, and ingestion from external data sources
The Pub/Sub client is the main entry point for interacting with Google Cloud Pub/Sub. It is scoped to a single Google Cloud project and provides methods for creating and accessing topics, subscriptions, and snapshots.
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error)Creates a new Pub/Sub client for the specified project.
Parameters:
ctx: Context for the operationprojectID: Google Cloud project ID, or DetectProjectID for automatic detectionopts: Optional client options (e.g., credentials, endpoint, connection pool settings)Returns: Pointer to Client and error
Example:
client, err := pubsub.NewClient(ctx, "my-project-id")
if err != nil {
log.Fatal(err)
}
defer client.Close()const DetectProjectID = "*detect-project-id*"Use this sentinel value to automatically detect the project ID from credentials:
client, err := pubsub.NewClient(ctx, pubsub.DetectProjectID)type ClientConfig struct {
PublisherCallOptions *vkit.PublisherCallOptions
SubscriberCallOptions *vkit.SubscriberCallOptions
EnableOpenTelemetryTracing bool
}Configure custom call options or enable OpenTelemetry tracing.
type Client struct {
// Has unexported fields
}The Client type provides methods for managing Pub/Sub resources.
func (c *Client) Close() errorCloses the client and releases all resources. Always defer Close() after creating a client.
func (c *Client) Project() stringReturns the project ID associated with the client.
func (c *Client) Topic(id string) *TopicReturns a reference to a topic in the client's project. Does not check if the topic exists.
Parameters:
id: Topic ID (not the full resource name)Returns: Pointer to Topic
func (c *Client) TopicInProject(id, projectID string) *TopicReturns a reference to a topic in a specific project.
Parameters:
id: Topic IDprojectID: Project ID (can differ from client's project)Returns: Pointer to Topic
func (c *Client) Subscription(id string) *SubscriptionReturns a reference to a subscription in the client's project.
Parameters:
id: Subscription ID (not the full resource name)Returns: Pointer to Subscription
func (c *Client) SubscriptionInProject(id, projectID string) *SubscriptionReturns a reference to a subscription in a specific project.
Parameters:
id: Subscription IDprojectID: Project ID (can differ from client's project)Returns: Pointer to Subscription
func (c *Client) Snapshot(id string) *SnapshotReturns a reference to a snapshot in the client's project.
Parameters:
id: Snapshot IDReturns: Pointer to Snapshot
func (c *Client) CreateTopic(ctx context.Context, topicID string) (*Topic, error)Creates a new topic with default configuration.
Parameters:
ctx: Context for the operationtopicID: ID for the new topicReturns: Pointer to created Topic and error
Example:
topic, err := client.CreateTopic(ctx, "my-topic")
if err != nil {
log.Fatal(err)
}func (c *Client) CreateTopicWithConfig(ctx context.Context, topicID string, tc *TopicConfig) (*Topic, error)Creates a new topic with custom configuration.
Parameters:
ctx: Context for the operationtopicID: ID for the new topictc: Topic configuration (labels, schema settings, retention, etc.)Returns: Pointer to created Topic and error
Example:
topic, err := client.CreateTopicWithConfig(ctx, "my-topic", &pubsub.TopicConfig{
Labels: map[string]string{
"env": "production",
},
RetentionDuration: 24 * time.Hour,
})func (c *Client) CreateSubscription(ctx context.Context, id string, cfg SubscriptionConfig) (*Subscription, error)Creates a new subscription with the specified configuration.
Parameters:
ctx: Context for the operationid: ID for the new subscriptioncfg: Subscription configuration including topic, ack deadline, delivery settings, etc.Returns: Pointer to created Subscription and error
Example:
sub, err := client.CreateSubscription(ctx, "my-subscription", pubsub.SubscriptionConfig{
Topic: topic,
AckDeadline: 20 * time.Second,
})
if err != nil {
log.Fatal(err)
}func (c *Client) Topics(ctx context.Context) *TopicIteratorReturns an iterator for listing all topics in the project.
Parameters:
ctx: Context for the operationReturns: Pointer to TopicIterator
Example:
it := client.Topics(ctx)
for {
topic, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(topic.ID())
}func (c *Client) Subscriptions(ctx context.Context) *SubscriptionIteratorReturns an iterator for listing all subscriptions in the project.
Parameters:
ctx: Context for the operationReturns: Pointer to SubscriptionIterator
Example:
it := client.Subscriptions(ctx)
for {
sub, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Println(sub.ID())
}func (c *Client) Snapshots(ctx context.Context) *SnapshotConfigIteratorReturns an iterator for listing all snapshots in the project.
Parameters:
ctx: Context for the operationReturns: Pointer to SnapshotConfigIterator
type TopicIterator struct {
// Has unexported fields
}
func (it *TopicIterator) Next() (*Topic, error)
func (it *TopicIterator) NextConfig() (*TopicConfig, error)Iterator for listing topics. Call Next() repeatedly until it returns iterator.Done.
type SubscriptionIterator struct {
// Has unexported fields
}
func (it *SubscriptionIterator) Next() (*Subscription, error)
func (it *SubscriptionIterator) NextConfig() (*SubscriptionConfig, error)Iterator for listing subscriptions. Call Next() repeatedly until it returns iterator.Done.
type SnapshotConfigIterator struct {
// Has unexported fields
}
func (it *SnapshotConfigIterator) Next() (*SnapshotConfig, error)Iterator for listing snapshot configurations.
Customize the gRPC connection pool size for high-throughput scenarios:
import "google.golang.org/api/option"
opts := []option.ClientOption{
option.WithGRPCConnectionPool(2),
}
client, err := pubsub.NewClient(ctx, projectID, opts...)The default connection pool size is min(4, GOMAXPROCS). Each connection supports up to 100 streams.
const (
ScopePubSub = "https://www.googleapis.com/auth/pubsub"
ScopeCloudPlatform = "https://www.googleapis.com/auth/cloud-platform"
)OAuth2 scopes for Pub/Sub access. The library automatically uses the appropriate scope.
Set the PUBSUB_EMULATOR_HOST environment variable to use a Pub/Sub emulator:
import "os"
err := os.Setenv("PUBSUB_EMULATOR_HOST", "localhost:8085")
if err != nil {
log.Fatal(err)
}
// Client will now connect to the emulator
client, err := pubsub.NewClient(ctx, "my-project-id")var ErrEmptyProjectID = errors.New("pubsub: project ID is empty")Returned when an empty project ID is provided and automatic detection is not used.