go-samber-ro
Reactive programming in Go with samber/ro. Covers streams, observables, observers, and type-safe operators.
SKILL.mdskills/go/go-samber-ro/SKILL.md
GitHubCore Philosophy
samber/ro is a Go implementation of ReactiveX (Reactive Extensions). It provides a way to compose asynchronous and event-based programs using observable sequences.
- Asynchronous: Handles streams of data that arrive over time (e.g., WebSockets, tickers).
- Type-safe: Uses Go Generics for all operators.
- Composable: Chain operators to filter, transform, and combine streams.
- Backpressure: Built-in mechanisms to handle producers that are faster than consumers.
Core Components
- Observable: The data source that emits a sequence of items.
- Observer: The consumer that reacts to items (
OnNext), errors (OnError), and completion (OnComplete). - Operators: Functions like
Map,Filter,Merge, andDebouncethat transform the stream. - Subscription: Represents the connection between an observer and an observable. Use it to cancel the stream.
Basic Usage
Creating a Stream
import "github.com/samber/ro"
observable := ro.Pipe2(
ro.FromSlice([]int{1, 2, 3, 4, 5}),
ro.Filter(func(x int) bool { return x%2 == 0 }),
ro.Map(func(x int) string { return fmt.Sprintf("value: %d", x) }),
)
Subscribing
observable.Subscribe(ro.NewObserver(
func(s string) { fmt.Println(s) }, // OnNext
func(err error) { log.Fatal(err) }, // OnError
func() { fmt.Println("Completed!") }, // OnComplete
))
Common Operators
| Category | Operators |
|---|---|
| Creation | Just, FromSlice, FromChannel, Interval, Range. |
| Filtering | Filter, Distinct, Debounce, Throttle, Take, Skip. |
| Transformation | Map, FlatMap, Scan, Buffer. |
| Combination | Merge, Zip, CombineLatest, Concat. |
| Error Handling | Catch, Retry, OnErrorReturn. |
Why use ro?
- Declarative: Focus on what should happen to the data rather than how to manage goroutines and channels.
- Complex Coordination: Easily combine multiple async sources (e.g., "Wait for both a DB result and an API response before proceeding").
- Time-Aware: Operators like
DebounceandIntervalmake handling time-based logic trivial. - Consistency: Follows a well-known pattern used in many other languages (RxJS, RxJava, etc.).
Best Practices
- Always Handle Errors — Use
NewObserverwith all three callbacks to ensure errors don't go unnoticed. - Unsubscribe — Always clean up subscriptions (or use
TakeUntil) to prevent goroutine leaks. - Cold vs Hot — Understand that by default, each subscriber gets a fresh execution. Use
Share()to multicasts a single execution to multiple subscribers. - Use Typed Pipes — Prefer
Pipe2,Pipe3, etc., for better compile-time type checking. - Context Integration — Use
ContextWithTimeoutto automatically terminate streams when a Go context expires.
Anti-patterns
- ❌ Using
rofor simple Slices — If your data is finite and already in memory, usesamber/lo.rois for data over time. - ❌ Ignoring
OnComplete— Some operators or logic might depend on the stream finishing; don't forget to implement the completion callback. - ❌ Manual Channel Management — If you're using
ro, try to stay within its ecosystem rather than manually piping channels in and out. - ❌ Deep Nesting — Keep your pipelines readable by breaking them into smaller, named observables.