Junior — Middle
Have you used Kafka Producer in your projects?
sobes.tech AI
Answer from AI
Yes, I have used Kafka Producer in my Go projects to send messages to Kafka topics. I usually used the segmentio/kafka-go library, which allows for simple and efficient integration with Kafka. Here's an example of creating and sending a message:
package main
import (
"context"
"log"
"github.com/segmentio/kafka-go"
)
func main() {
writer := kafka.NewWriter(kafka.WriterConfig{
Brokers: []string{"localhost:9092"},
Topic: "example-topic",
})
defer writer.Close()
err := writer.WriteMessages(context.Background(),
kafka.Message{
Key: []byte("Key-A"),
Value: []byte("Hello Kafka from Go!"),
},
)
if err != nil {
log.Fatal("failed to write messages:", err)
}
}
This approach allows for asynchronous data sending and building scalable message exchange systems.