ctxcli is a Go library extending the standard context with handling for OS signals.
- Create a child context that cancels when an OS signal like SIGINT is received.
- Helper functions to exit or panic when the provided context is cancelled.
- Consistent API with the context standard library.
package main
import (
"context"
"log"
"time"
"github.com/jhulten/go-ctxcli"
)
func main() {
ctx := context.Background()
ctx = ctxcli.WithInterrupt(ctx)
for {
log.Print(".")
time.Sleep(500 * time.Millisecond)
ctxcli.ExitIfCancelled(ctx)
}
}