Go

In this tutorial, you will run a Golang client application that produces messages to and consumes messages from an Apache Kafka® cluster.

After you run the tutorial, view the provided source code and use it as a reference to develop your own Kafka client application.

Prerequisites

Client

Kafka Cluster

  • You can use this tutorial with a Kafka cluster in any environment:
  • If you are running on Confluent Cloud, you must have access to a Confluent Cloud cluster
    • The first 20 users to sign up for Confluent Cloud and use promo code C50INTEG will receive an additional $50 free usage (details)

Setup

  1. Clone the confluentinc/examples GitHub repository and check out the 5.5.1-post branch.

    git clone https://github.com/confluentinc/examples
    cd examples
    git checkout 5.5.1-post
    
  2. Change directory to the example for Go.

    cd clients/cloud/go/
    
  3. Create a local file (for example, at $HOME/.confluent/librdkafka.config) with configuration parameters to connect to your Kafka cluster. Starting with one of the templates below, customize the file with connection information to your cluster. Substitute your values for {{ BROKER_ENDPOINT }}, {{CLUSTER_API_KEY }}, and {{ CLUSTER_API_SECRET }} (see Connecting Clients to Confluent Cloud for instructions on how to create or find those values).

    • Template configuration file for Confluent Cloud

      # Kafka
      bootstrap.servers={{ BROKER_ENDPOINT }}
      security.protocol=SASL_SSL
      sasl.mechanisms=PLAIN
      sasl.username={{ CLUSTER_API_KEY }}
      sasl.password={{ CLUSTER_API_SECRET }}
      
    • Template configuration file for local host

      # Kafka
      bootstrap.servers=localhost:9092
      

Basic Producer and Consumer

In this example, the producer application writes Kafka data to a topic in your Kafka cluster. If the topic does not already exist in your Kafka cluster, the producer application will use the Kafka Admin Client API to create the topic. Each record written to Kafka has a key representing a username (for example, alice) and a value of a count, formatted as json (for example, {“Count”: 0}). The consumer application reads the same Kafka topic and keeps a rolling sum of the count as it processes each record.

Produce Records

  1. Build the producer application.

    go build producer.go
    
  2. Run the producer, passing in arguments for:

    • the local file with configuration parameters to connect to your Kafka cluster
    • topic name
    ./producer -f $HOME/.confluent/librdkafka.config -t test1
    
  3. Verify the producer sent all the messages. You should see:

    Preparing to produce record: alice   {"Count": 0}
    Preparing to produce record: alice   {"Count": 1}
    Preparing to produce record: alice   {"Count": 2}
    Preparing to produce record: alice   {"Count": 3}
    Preparing to produce record: alice   {"Count": 4}
    Preparing to produce record: alice   {"Count": 5}
    Preparing to produce record: alice   {"Count": 6}
    Preparing to produce record: alice   {"Count": 7}
    Preparing to produce record: alice   {"Count": 8}
    Preparing to produce record: alice   {"Count": 9}
    Successfully produced record to topic test1 partition [0] @ offset 0
    Successfully produced record to topic test1 partition [0] @ offset 1
    Successfully produced record to topic test1 partition [0] @ offset 2
    Successfully produced record to topic test1 partition [0] @ offset 3
    Successfully produced record to topic test1 partition [0] @ offset 4
    Successfully produced record to topic test1 partition [0] @ offset 5
    Successfully produced record to topic test1 partition [0] @ offset 6
    Successfully produced record to topic test1 partition [0] @ offset 7
    Successfully produced record to topic test1 partition [0] @ offset 8
    Successfully produced record to topic test1 partition [0] @ offset 9
    10 messages were produced to topic test1!
    
  4. View the producer code.

Consume Records

  1. Build the producer application.

    go build consumer.go
    
  2. Run the consumer, passing in arguments for:

    • the local file with configuration parameters to connect to your Kafka cluster
    • the same topic name used in step 1
    ./consumer -f $HOME/.confluent/librdkafka.config -t test1
    
  3. Verify the consumer received all the messages. You should see:

    ...
    Consumed record with key alice and value {"Count":0}, and updated total count to 0
    Consumed record with key alice and value {"Count":1}, and updated total count to 1
    Consumed record with key alice and value {"Count":2}, and updated total count to 3
    Consumed record with key alice and value {"Count":3}, and updated total count to 6
    Consumed record with key alice and value {"Count":4}, and updated total count to 10
    Consumed record with key alice and value {"Count":5}, and updated total count to 15
    Consumed record with key alice and value {"Count":6}, and updated total count to 21
    Consumed record with key alice and value {"Count":7}, and updated total count to 28
    Consumed record with key alice and value {"Count":8}, and updated total count to 36
    Consumed record with key alice and value {"Count":9}, and updated total count to 45
    ...
    
  4. View the consumer code.