Created
May 18, 2015 05:50
-
-
Save tylertreat/14b5b0ac98134e06cf5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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