Get list of topics in kafka Golang

Get list of topics in kafka Golang

There are different library available in Golang to publish and subscribe topics in kafka. In this example, we are using sarama library to list all the available topic at kafka broker.

To get sarama library, run below command.

go get

Code to list all topics.

package main

import (


func main() {
	config := sarama.NewConfig()
	config.Consumer.Return.Errors = true

	//kafka end point
	brokers := []string{"localhost:9093"}

	//get broker
	cluster, err := sarama.NewConsumer(brokers, config)
	if err != nil {

	defer func() {
		if err := cluster.Close(); err != nil {

	//get all topic from cluster
	topics, _ := cluster.Topics()
	for index := range topics {

Running above code produces below output on my machine. Because, I have two topics(Test – 1 and Test-2) available with my kafka broker.



Leave a Reply

Your email address will not be published. Required fields are marked *