Sobes.tech
Junior — Middle

What library have you used for interacting with Kafka?

sobes.tech AI

Answer from AI

The library Shopify/sarama is often used for interacting with Kafka in Go. It is a popular and well-supported Kafka client for Go, providing a full set of features for producers and consumers.

Example of a simple producer using sarama:

package main

import (
    "log"
    "github.com/Shopify/sarama"
)

func main() {
    producer, err := sarama.NewSyncProducer([]string{"localhost:9092"}, nil)
    if err != nil {
        log.Fatal("Error creating producer", err)
    }
    defer producer.Close()

    msg := &sarama.ProducerMessage{
        Topic: "test_topic",
        Value: sarama.StringEncoder("Hello Kafka"),
    }

    partition, offset, err := producer.SendMessage(msg)
    if err != nil {
        log.Fatal("Failed to send message", err)
    }

    log.Printf("Message sent to partition %d at offset %d", partition, offset)
}

Besides sarama, there are other libraries such as segmentio/kafka-go, but sarama is one of the most widely used.