Skip to content

Instantly share code, notes, and snippets.

@myself659
Created April 7, 2019 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myself659/161e6310b485e2b08b352a9305349deb to your computer and use it in GitHub Desktop.
Save myself659/161e6310b485e2b08b352a9305349deb to your computer and use it in GitHub Desktop.
goroutine sync
package main
import "fmt"
func gPrint(ch chan<- string, s string, sig chan struct{}) {
for {
select {
case ch <- s:
{
// do nothing
}
case <-sig:
{
return
}
}
}
}
func Sync() {
cha := make(chan string)
chb := make(chan string)
chc := make(chan string)
sig := make(chan struct{})
go gPrint(cha, "A", sig)
go gPrint(chb, "B", sig)
go gPrint(chc, "C", sig)
for i := 0; i < 10; i++ {
fmt.Println(i, ":--")
fmt.Println(<-cha)
fmt.Println(<-chb)
fmt.Println(<-chc)
}
close(sig)
}
func main(){
Sync()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment