genericpubsub is a lightweight, type-safe publish/subscribe system for Go, using generics and context-based
cancellation. It allows you to publish messages of any type to multiple subscribers concurrently, with clean shutdown
and resource management.
- Type-safe using Go generics
- Simple API:
Send,Subscribe - Context-based cancellation
- Graceful shutdown of subscribers
- Fully tested with unit tests
go get github.com/sesopenko/genericpubsubhttps://pkg.go.dev/github.com/sesopenko/genericpubsub
package main
import (
"context"
"fmt"
"time"
"github.com/sesopenko/genericpubsub"
)
type Message struct {
Value string
}
func main() {
channelBuffer := 64
ps := genericpubsub.New[Message](context.Background(), channelBuffer)
sub := ps.Subscribe(context.TODO(), channelBuffer)
go ps.Send(Message{Value: "hello"})
time.Sleep(50 * time.Millisecond)
msg, ok := <-sub
fmt.Println("Received:", msg.Value)
fmt.Printf("channel wasn't closed: %t\n", ok)
}This project is licensed under the MIT license. See LICENSE.txt for details.
© 2025 Sean Esopenko