Created
May 16, 2018 17:14
-
-
Save philangist/d14bc3319e1eb4a9c9f983f6dd461fc0 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"fmt" | |
) | |
func write(input chan int) { | |
for i := 0; i < 4; i++ { | |
fmt.Printf("sending i: %d to input channel\n", i) | |
input <- i | |
} | |
} | |
func read(input, err chan int) { | |
for { | |
i := <-input | |
fmt.Printf("read i: %d from input channel\n", i) | |
if i%3 == 0 { | |
fmt.Printf("send i: %d to err channel\n", i) | |
err <- i | |
} | |
if i == -1 { | |
defer close(input) | |
defer close(err) | |
return | |
} | |
} | |
} | |
func main() { | |
ch := make(chan int, 3) | |
err := make(chan int, 1) | |
go read(ch, err) | |
go write(ch) | |
i := 0 | |
for value, ok := <- err; ok; value, ok = <- err { | |
fmt.Printf("read value: %d from err\n", value) | |
i += 1 | |
if i >= 3 { | |
ch <- value * 1000 | |
break | |
} | |
ch <- value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment