MCPcopy
hub / github.com/nats-io/nats.go / main

Function main

examples/nats-qsub/main.go:43–117  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41}
42
43func main() {
44 var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
45 var userCreds = flag.String("creds", "", "User Credentials File")
46 var nkeyFile = flag.String("nkey", "", "NKey Seed File")
47 var showTime = flag.Bool("t", false, "Display timestamps")
48 var showHelp = flag.Bool("h", false, "Show help message")
49
50 log.SetFlags(0)
51 flag.Usage = usage
52 flag.Parse()
53
54 if *showHelp {
55 showUsageAndExit(0)
56 }
57
58 args := flag.Args()
59 if len(args) != 2 {
60 showUsageAndExit(1)
61 }
62
63 // Connect Options.
64 opts := []nats.Option{nats.Name("NATS Sample Queue Subscriber")}
65 opts = setupConnOptions(opts)
66
67 if *userCreds != "" && *nkeyFile != "" {
68 log.Fatal("specify -seed or -creds")
69 }
70
71 // Use UserCredentials
72 if *userCreds != "" {
73 opts = append(opts, nats.UserCredentials(*userCreds))
74 }
75
76 // Use Nkey authentication.
77 if *nkeyFile != "" {
78 opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
79 if err != nil {
80 log.Fatal(err)
81 }
82 opts = append(opts, opt)
83 }
84
85 // Connect to NATS
86 nc, err := nats.Connect(*urls, opts...)
87 if err != nil {
88 log.Fatal(err)
89 }
90
91 subj, queue, i := args[0], args[1], 0
92
93 nc.QueueSubscribe(subj, queue, func(msg *nats.Msg) {
94 i++
95 printMsg(msg, i)
96 })
97 nc.Flush()
98
99 if err := nc.LastError(); err != nil {
100 log.Fatal(err)

Callers

nothing calls this directly

Calls 11

ConnectMethod · 0.80
FatalfMethod · 0.80
showUsageAndExitFunction · 0.70
setupConnOptionsFunction · 0.70
printMsgFunction · 0.70
NameMethod · 0.65
QueueSubscribeMethod · 0.65
DrainMethod · 0.65
StringMethod · 0.45
FlushMethod · 0.45
LastErrorMethod · 0.45

Tested by

no test coverage detected