Created
September 4, 2018 11:49
-
-
Save rogerwelin/2c23fc3f25305de218919c5844804714 to your computer and use it in GitHub Desktop.
unbuffered-channel
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
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| func printer(msg string, ch chan<- string) { | |
| ch <- msg | |
| } | |
| func main() { | |
| stuff := []string{"one", "two", "three", "four", "five", "six", "seven"} | |
| stringStream := make(chan string) | |
| fmt.Println("init") | |
| go func() { | |
| for _, item := range stuff { | |
| printer(item, stringStream) | |
| } | |
| close(stringStream) | |
| }() | |
| for item := range stringStream { | |
| fmt.Println(item) | |
| } | |
| fmt.Println("end") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment