producer
package main
import (
"context"
"fmt"
"log"
pulsar "github.com/apache/pulsar-client-go/pulsar"
)
func main() {
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: "pulsar://localhost:6650",
})
if err != nil {
log.Fatal(err)
}
defer client.Close()
producer, err := client.CreateProducer(pulsar.ProducerOptions{
Topic: "my-topic",
})
if err != nil {
log.Fatal(err)
}
defer producer.Close()
msg := &pulsar.ProducerMessage{
Payload: []byte("Hello, Pulsar!"),
}
_, err = producer.Send(context.Background(), msg)
if err != nil {
log.Fatal(err)
}
fmt.Println("Message sent successfully")
}
consumer
package main
import (
"context"
"fmt"
"log"
pulsar "github.com/apache/pulsar-client-go/pulsar"
)
func main() {
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: "pulsar://localhost:6650",
})
if err != nil {
log.Fatal(err)
}
defer client.Close()
consumer, err := client.Subscribe(pulsar.ConsumerOptions{
Topic: "my-topic",
SubscriptionName: "my-subscription",
Type: pulsar.Shared,
})
if err != nil {
log.Fatal(err)
}
defer consumer.Close()
msg, err := consumer.Receive(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("Received message: %s\n", string(msg.Payload()))
consumer.Ack(msg)
}