Skip to content
Oeiuwq Faith Blog OpenSource Porfolio

sesopenko/genericpubsub

A lightweight, type-safe publish/subscribe system for Go, using generics and context-based cancellation

sesopenko/genericpubsub.json
{
"createdAt": "2025-04-18T05:45:17Z",
"defaultBranch": "main",
"description": "A lightweight, type-safe publish/subscribe system for Go, using generics and context-based cancellation",
"fullName": "sesopenko/genericpubsub",
"homepage": null,
"language": "Go",
"name": "genericpubsub",
"pushedAt": "2025-04-18T06:41:28Z",
"stargazersCount": 18,
"topics": [],
"updatedAt": "2025-10-16T23:02:14Z",
"url": "https://github.com/sesopenko/genericpubsub"
}

Go Tests Go Reference GitHub tag [License: MIT]!(LICENSE.txt) Go Version

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
Terminal window
go get github.com/sesopenko/genericpubsub

https://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]!(LICENSE.txt) for details.

© 2025 Sean Esopenko