package with import ( "context" "os/signal" "syscall" ) func Context(foo func(context.Context) error) error { ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT) defer can() if err := foo(ctx); err != nil && ctx.Err() == nil { return err } return nil }