PubSub is a Go Package for in-memory publish/subscribe.
go get github.com/davidscottmills/pubsubpackage main()
import (
"fmt"
"github.com/davidscottmills/pubsub"
)
var ps *PubSub
func main() {
ps := NewPubSub()
handerFunc := func(m *pubsub.Msg) {
fmt.Println(m.Data)
}
// Subscribe to a subject
s, err := ps.Subscribe("subject.name", handerFunc)
defer s.Unsubscribe()
// Publish
err := ps.Publish("subject.name", "Hello, world!")
someStruct := struct{}{}
// Publish any type you'd like
ps.Publish("subject.name", someStruct)
runAndBlock()
}Subject naming matches that of NATS.
- All ascii alphanumeric characters except spaces/tabs and separators which are "." and ">" are allowed. Subject names can be optionally token-delimited using the dot character (.)
- Subjects are case sensative
- The asterisk character (*) matches a single token at any level of the subject.
- The greater than symbol (>), also known as the full wildcard, matches one or more tokens at the tail of a subject, and must be the last token. The wildcarded subject foo.> will match foo.bar or foo.bar.baz.1, but not foo.
- Wildcards must be a separate token (foo..baz or foo.> are syntactically valid; foo.bar, fo.br and foo> are not)
- Implement and test close or drain on PubSub
- Implement and test errors