confluent-kafka-go icon indicating copy to clipboard operation
confluent-kafka-go copied to clipboard

Producer:what's the event type of event delivered by delivery_chan

Open huangchong94 opened this issue 1 year ago • 0 comments

Description

delivery_chan := make(chan kafka.Event, 10000)
err = p.Produce(&kafka.Message{
    TopicPartition: kafka.TopicPartition{Topic: topic, Partition: kafka.PartitionAny},
    Value: []byte(value)},
    delivery_chan
)

 e := <-delivery_chan
 m := e.(*kafka.Message)

 if m.TopicPartition.Error != nil {
     fmt.Printf("Delivery failed: %v\n", m.TopicPartition.Error)
 } else {
     fmt.Printf("Delivered message to topic %s [%d] at offset %v\n",
             *m.TopicPartition.Topic, m.TopicPartition.Partition, m.TopicPartition.Offset)
 }
 close(delivery_chan)

for this simple example, copy from confluent official site will this line m := e.(*kafka.Message) panic ?, because e is another type.

Should I write a for loop to wait for an event whose type is *kafka.Message

for e := range delivery_chan{
// do type switch, if type is *kafka.Message , log  error if any and return
}

huangchong94 avatar Jan 01 '24 13:01 huangchong94