Help improve this documentation
This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
signal operatorsโ
This page lists all operators available in the signal sub-package of ro.
Installโ
First, import the sub-package in your project:
go get -u github.com/samber/ro/plugins/signal
NewSignalCatcherโ
Catches system signals and emits them as observable values.
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/samber/ro"
rosignal "github.com/samber/ro/plugins/signal"
)
obs := rosignal.NewSignalCatcher(syscall.SIGINT, syscall.SIGTERM)
sub := obs.Subscribe(ro.NewObserver[os.Signal](
func(sig os.Signal) {
fmt.Printf("Received signal: %v\n", sig)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Signal stream completed")
},
))
defer sub.Unsubscribe()
// When user presses Ctrl+C:
// Received signal: interrupt
// CompletedIf no signals are provided, all incoming signals will be relayed. The observable completes when the signal channel is closed.
Prototype:func NewSignalCatcher(signals ...os.Signal)