Skip to content

Instantly share code, notes, and snippets.

@zhengkai
Last active November 25, 2016 09:04
Show Gist options
  • Save zhengkai/fad7cfcb6c88834beeaaf74cfe4da376 to your computer and use it in GitHub Desktop.
Save zhengkai/fad7cfcb6c88834beeaaf74cfe4da376 to your computer and use it in GitHub Desktop.
Go Channel Tour
package main
import (
"fmt"
"time"
)
func main() {
cA := make(chan string)
go a(&cA)
cB := make(chan string)
go b(&cB)
i := 0
for {
i++
s := fmt.Sprintf(`%v`, i)
cA <- s
cB <- s
time.Sleep(1 * time.Second)
}
}
func a(ch *chan string) {
for {
s := <-*ch
fmt.Println(`a`, s)
// time.Sleep(5 * time.Second)
}
}
func b(ch *chan string) {
for {
s := <-*ch
fmt.Println(`b`, s)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment