DemoPublic/Midware/NATS/Client/Golang/natssub.go

26 lines
358 B
Go
Raw Normal View History

package main
import (
"fmt"
"sync"
"github.com/nats-io/nats.go"
)
func main() {
nc, err := nats.Connect(nats.DefaultURL)
defer nc.Close()
if nil != err {
fmt.Println(err)
return
}
wg := sync.WaitGroup{}
wg.Add(10)
nc.Subscribe("foo", func(m *nats.Msg) {
fmt.Printf("Received a message: %s\n", string(m.Data))
wg.Done()
})
wg.Wait()
}