Skip to content

Instantly share code, notes, and snippets.

View shicky's full-sized avatar

Steve shicky

View GitHub Profile
@joshlf
joshlf / doer.go
Created October 2, 2016 17:05
A demonstration of a self-cleaning worker
// This is an example of a self-cleaning worker. A common pattern in Go
// is to have a type which, when created with NewX, spawns one or more
// goroutines in the background to perform work. It is usually the
// caller's responsibility to call Close() or Stop() in order to shut
// these workers down and not leak goroutines and the memory resources
// that the goroutines have references to.
//
// The idea behind a self-cleaning worker is to leverage the runtime's
// ability to set finalizers on objects in order to detect when an object
// with a still-live worker goroutine has gone out of scope. The type