aslrousta / pubsub

Generic PubSub Pattern in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PubSub

This library provides a generic implementation of pub-sub pattern using channels in Go. An example usage would be like:

ps := pubsub.New[string, int]()

// Produces a random integer each second and publishes
// it on the "random" topic.
go func() {
    for {
        ps.Publish("random", rand.Int())
        time.Sleep(time.Second)
    }
}()

// Consumes produced random integers by subscribing on
// the "random" topic.
go func() {
    ch := make(chan int)
    ps.Subscribe("random", ch)
    for r := range ch {
        fmt.Println(r)
    }
}()

About

Generic PubSub Pattern in Go

License:MIT License


Languages

Language:Go 100.0%