Skip to content

Instantly share code, notes, and snippets.

@JnBrymn
Last active August 29, 2015 14:03
Show Gist options
  • Save JnBrymn/6fc38872b4d312886908 to your computer and use it in GitHub Desktop.
Save JnBrymn/6fc38872b4d312886908 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/Shopify/sarama" //GIT hash = b3d9702dd2d2cfe6b85b9d11d6f25689e6ef24b0
"time"
)
var groupName = "trash"
var topicName = "event"
var partition int32 = 0
var client *sarama.Client
var err error
func clientStart() {
fmt.Println("creating client")
var err error
client, err = sarama.NewClient(groupName, []string{"localhost:9092"}, nil)
if err != nil {
fmt.Println("ERROR:", err)
}
}
func consumer() {
fmt.Println("creating consumer")
consumer, err := sarama.NewConsumer(client, topicName, partition, groupName, nil)
if err != nil {
fmt.Println("ERROR:", err)
}
for e := range consumer.Events() {
fmt.Println(string(e.Value))
}
}
func producer() {
fmt.Println("creating producer")
producer, err := sarama.NewProducer(client, nil)
if err != nil {
fmt.Println("ERROR:", err)
}
for i := 0; i < 10; i++ {
producer.SendMessage(topicName, nil, sarama.StringEncoder(fmt.Sprintf("A%d", i)))
}
}
func main() {
clientStart()
go consumer()
time.Sleep(time.Second)
go producer()
<-make(chan int)
}
@cyrildmoses
Copy link

Hi JnBrymn - When I tried to execute the above code, i got the below error,. Can you plz help me?

command-line-arguments

.\kafka.go:18: too many arguments in call to sarama.NewClient
.\kafka.go:18: cannot assign sarama.Client to client (type *sarama.Client) in mu
ltiple assignment:
*sarama.Client is pointer to interface, not interface
.\kafka.go:26: too many arguments in call to sarama.NewConsumer
.\kafka.go:31: consumer.Events undefined (type sarama.Consumer has no field or m
ethod Events)
.\kafka.go:38: undefined: sarama.NewProducer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment