Debian Codegraph @

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

raw
1 package main
2
3 import "github.com/nats-io/nats.go"
4
5 func main() {
6 nc, err := nats.Connect(nats.DefaultURL)
7 if err != nil {
8 panic(err)
9 }
10 defer nc.Close()
11
12 // NATS-DOC-START
13 // Publish one order to the orders.created subject. Publishing is
14 // fire-and-forget: the call hands the message to the server and returns.
15 order := `{"order_id":"ord_8w2k","customer":"acme-co","total_cents":4200,"ts":"2026-05-22T10:14:22Z"}`
16 nc.Publish("orders.created", []byte(order))
17 // NATS-DOC-END
18
19 // Flush so the buffered publish reaches the server before we exit.
20 nc.Flush()
21 }