Debian Codegraph @

examples/docs/learn-core-nats-publish-subscribe-subscribe/main.go

raw
1 package main
2
3 import (
4 "fmt"
5
6 "github.com/nats-io/nats.go"
7 )
8
9 func main() {
10 nc, err := nats.Connect(nats.DefaultURL)
11 if err != nil {
12 panic(err)
13 }
14 defer nc.Close()
15
16 // NATS-DOC-START
17 // Subscribe as the warehouse service to orders.created. The callback runs
18 // for each matching message as it is published.
19 nc.Subscribe("orders.created", func(m *nats.Msg) {
20 fmt.Printf("warehouse received: %s\n", string(m.Data))
21 })
22 // NATS-DOC-END
23
24 // Block so the subscriber keeps running.
25 select {}
26 }