Skip to content

Instantly share code, notes, and snippets.

@duythinht
Created December 2, 2016 08:54
Show Gist options
  • Save duythinht/5813b0a5668d3d73c962cb5c2159b017 to your computer and use it in GitHub Desktop.
Save duythinht/5813b0a5668d3d73c962cb5c2159b017 to your computer and use it in GitHub Desktop.
println! still done!
package main
import (
"fmt"
"os"
"time"
)
func println(id int, done chan bool, messages <-chan string, result chan int) {
for message := range messages {
time.Sleep(1 * time.Second)
fmt.Println(id, ":", message)
}
done <- true
}
func mc() chan string {
messages := make(chan string, 4)
return messages
}
func main() {
messages := mc()
done := make(chan bool)
result := make(chan int)
n := 4
for w := 1; w <= 4; w++ {
go println(w, done, messages, result)
}
for i := 0; i < 10; i++ {
select {
case v := <-result:
fmt.Println(v)
os.Exit(0)
default:
messages <- fmt.Sprintf("Hello %d", i)
}
}
close(messages)
for {
<-done
if n--; n == 0 {
fmt.Println("All done")
os.Exit(1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment