Documentation ¶
Overview ¶
Package cancelable provides a cancelable version of io.Closer.
This is intended to be used by functions where the responsibility for closing a resource sometimes belongs to the function itself (e.g. early returns due to error handling), and sometimes belongs to the caller.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Closer ¶
type Closer struct {
// contains filtered or unexported fields
}
Closer is an io.Closer that can be cancelled.
Example ¶
package main import ( "fmt" "github.com/samuong/alpaca/cancelable" ) type printCloser struct { msg string } // Close prints the printCloser's msg on stdout. func (pc *printCloser) Close() error { fmt.Printf("Close() called: %s\n", pc.msg) return nil } func main() { c := create(false) // defer in create() prints once c.Close() // prints again c = create(true) // defer cancelled, no print c.Close() // prints } func create(doCancel bool) *printCloser { pc := &printCloser{fmt.Sprintf("doCancel: %v", doCancel)} closer := cancelable.NewCloser(pc) defer closer.Close() if !doCancel { return pc // deferred Close will cause msg to be printed } closer.Cancel() // Cancel will prevent Close being called from defer return pc }
Output: Close() called: doCancel: false Close() called: doCancel: false Close() called: doCancel: true
Click to show internal directories.
Click to hide internal directories.