Skip to content

Instantly share code, notes, and snippets.

@tylertreat
Created May 18, 2015 05:50
Show Gist options
  • Save tylertreat/14b5b0ac98134e06cf5e to your computer and use it in GitHub Desktop.
Save tylertreat/14b5b0ac98134e06cf5e to your computer and use it in GitHub Desktop.
type IntContainer []int
func (i IntContainer) Iterator(cancel <-chan struct{}) <-chan int {
ch := make(chan int)
go func() {
for _, val := range i {
select {
case ch <- val:
case <-cancel:
close(ch)
return
}
}
close(ch)
}()
return ch
}
func main() {
c := IntContainer([]int{1, 2, 3, 4, 5})
cancel := make(chan struct{})
for x := range c.Iterator(cancel) {
println(x)
break
}
close(cancel)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment