Creating a Go Application for Managing GCP Pub/Sub

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Creating a Publisher
  5. Creating a Subscriber
  6. Conclusion

Introduction

In this tutorial, we will walk through the process of creating a Go application for managing Google Cloud Pub/Sub. Pub/Sub is a messaging service offered by Google Cloud Platform (GCP) that allows you to publish and receive messages between independent applications. By the end of this tutorial, you will have a solid understanding of how to create publishers and subscribers using the Go programming language.

Prerequisites

Before starting this tutorial, make sure you have the following prerequisites:

  • Basic knowledge of Go programming language.
  • Google Cloud Platform account with Pub/Sub API enabled.
  • Go installed on your machine.

Setup

To begin, we need to set up our Go environment and import the necessary libraries.

  1. Create a new Go module by running the following command in your terminal:

    ```shell
    go mod init pubsub-demo
    ```
    
    This will initialize a new Go module named `pubsub-demo` in the current directory.
    
  2. Install the necessary packages by executing the following command:

    ```shell
    go get cloud.google.com/go/pubsub
    ```
    
    This will install the Pub/Sub package for Go.
    
  3. Import the required libraries in your Go code:

    ```go
    package main
    
    import (
        "context"
        "fmt"
        "os"
    
        "cloud.google.com/go/pubsub"
    )
    ```
    
    We import the `context` package for managing context and the `pubsub` package for interacting with the Pub/Sub service.
    

Creating a Publisher

Let’s start by creating a publisher that publishes messages to a Pub/Sub topic.

  1. Create a new function named publishMessage as follows:

    ```go
    func publishMessage(projectID, topicID, message string) error {
        ctx := context.Background()
    
        // Create a Pub/Sub client.
        client, err := pubsub.NewClient(ctx, projectID)
        if err != nil {
            return fmt.Errorf("failed to create client: %v", err)
        }
    
        // Get the topic.
        topic := client.Topic(topicID)
    
        // Publish the message.
        result := topic.Publish(ctx, &pubsub.Message{
            Data: []byte(message),
        })
        _, err = result.Get(ctx)
        if err != nil {
            return fmt.Errorf("failed to publish message: %v", err)
        }
    
        fmt.Printf("Published message: %s\n", message)
        return nil
    }
    ```
    
    This function takes three parameters: `projectID`, `topicID`, and `message`. It creates a Pub/Sub client, retrieves the topic using the provided `topicID`, and publishes the message to the topic.
    
  2. Add the following code to the main function in order to call the publishMessage function:

    ```go
    func main() {
        projectID := "your-project-id"
        topicID := "your-topic-id"
        message := "Hello, Pub/Sub!"
    
        err := publishMessage(projectID, topicID, message)
        if err != nil {
            fmt.Printf("Failed to publish message: %v", err)
            os.Exit(1)
        }
    }
    ```
    
    Replace `your-project-id` and `your-topic-id` with the actual project and topic IDs from your GCP account.
    
  3. Build and run the Go application:

    ```shell
    go build
    ./pubsub-demo
    ```
    
    You should see the message "Published message: Hello, Pub/Sub!" indicating that the message was sent successfully.
    

    Congratulations! You have created a publisher to send messages to a Pub/Sub topic.

Creating a Subscriber

Now, let’s create a subscriber that receives messages from a Pub/Sub subscription.

  1. Create a new function named receiveMessage as follows:

    ```go
    func receiveMessage(projectID, subscriptionID string) error {
        ctx := context.Background()
    
        // Create a Pub/Sub client.
        client, err := pubsub.NewClient(ctx, projectID)
        if err != nil {
            return fmt.Errorf("failed to create client: %v", err)
        }
    
        // Get the subscription.
        subscription := client.Subscription(subscriptionID)
    
        // Create a channel to receive messages.
        messages := make(chan *pubsub.Message)
    
        // Receive messages concurrently.
        go func() {
            err = subscription.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {
                messages <- msg
            })
            if err != nil {
                fmt.Printf("Error receiving message: %v", err)
            }
        }()
    
        // Print received messages.
        for msg := range messages {
            fmt.Printf("Received message: %s\n", string(msg.Data))
    
            // Acknowledge the message to mark it as processed.
            msg.Ack()
        }
    
        return nil
    }
    ```
    
    This function takes two parameters: `projectID` and `subscriptionID`. It creates a Pub/Sub client, retrieves the subscription using the provided `subscriptionID`, and sets up a channel to receive messages. It then receives messages concurrently and prints them. Finally, it acknowledges each message to mark it as processed.
    
  2. Add the following code to the main function in order to call the receiveMessage function:

    ```go
    func main() {
        projectID := "your-project-id"
        subscriptionID := "your-subscription-id"
    
        err := receiveMessage(projectID, subscriptionID)
        if err != nil {
            fmt.Printf("Failed to receive messages: %v", err)
            os.Exit(1)
        }
    }
    ```
    
    Replace `your-project-id` and `your-subscription-id` with the actual project and subscription IDs from your GCP account.
    
  3. Build and run the Go application:

    ```shell
    go build
    ./pubsub-demo
    ```
    
    The application will start listening for messages. Any messages published to the subscribed topic will be received and printed to the console.
    

    Congratulations! You have created a subscriber to receive messages from a Pub/Sub subscription.

Conclusion

In this tutorial, you learned how to create a Go application for managing Google Cloud Pub/Sub. You created a publisher to send messages to a Pub/Sub topic and a subscriber to receive messages from a Pub/Sub subscription. This opens up possibilities for building robust distributed systems and decoupled microservices using Pub/Sub as the messaging backbone. Explore the extensive functionality provided by the cloud.google.com/go/pubsub package to further enhance your Pub/Sub applications.