Last active
July 20, 2020 23:45
-
-
Save filipegorges/41b1a49554d9120d34b7e1a5dadb5b2f 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
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
wisdomPipe := make(chan string) | |
mantraPipe := make(chan string) | |
echoPipe := make(chan string) | |
donePipe := make(chan struct{}) | |
defer close(donePipe) | |
go func(done <-chan struct{}, in <-chan string, out chan string) { | |
defer close(wisdomPipe) | |
for wiseWords := range in { | |
select { | |
case out <- wiseWords: | |
fmt.Println(wiseWords) | |
case <-done: | |
return | |
} | |
} | |
}(donePipe, wisdomPipe, mantraPipe) | |
go func(done <-chan struct{}, in <-chan string, out chan string) { | |
defer close(mantraPipe) | |
for mantra := range in { | |
select { | |
case out <- strings.ToUpper(mantra): | |
case <-done: | |
return | |
} | |
} | |
}(donePipe, mantraPipe, echoPipe) | |
go func(done <-chan struct{}, in <-chan string) { | |
defer close(echoPipe) | |
for echo := range in { | |
select { | |
case <-done: | |
return | |
default: | |
fmt.Println(echo) | |
} | |
} | |
}(donePipe, echoPipe) | |
wordsOfWisdom := []string{ | |
"Do not communicate by sharing memory;", | |
"instead, share memory by communicating.", | |
} | |
for _, words := range wordsOfWisdom { | |
wisdomPipe <- words | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment