You can use this simple function to verify existence of a Kafka topic in Golang.


type Kafka struct {
config *sarama.Config
brokers []string
}

func (k Kafka) CheckTopics(topics []string) error {
admin, err := sarama.NewClusterAdmin(k.brokers, k.config)
if err != nil {
return err
}
defer admin.Close()

list, err := admin.ListTopics()
if err != nil {
return err
}

for _, topic := range topics {
if _, ok := list[topic]; !ok {
return fmt.Errorf("topic is not found: %s", topic)
}
}

return nil
}