ctx, cancel := context.WithCancel(context.Background()) defer cancel() go func() { // perform some time-consuming operation if err != nil { cancel() } }() // wait for cancellation signal <-ctx.Done()
func doSomething(ctx context.Context, arg1, arg2 string) error { ctx, cancel := context.WithTimeout(ctx, time.Duration(5)*time.Second) defer cancel() // perform some operation that should be cancelled if it takes more than 5 seconds return nil }Package library: This package belongs to the camlistore.org/pkg package library, which contains several low-level packages used by the Camlistore server to implement its functionality.