Skip to content

Instantly share code, notes, and snippets.

@sugilog
Created January 24, 2015 14:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sugilog/262f63536317dbf48dd7 to your computer and use it in GitHub Desktop.
Goroutines:Channels
package main
import (
"fmt"
"time"
)
func ccounter( step int, channel chan int ) {
sum := 0
for i := 0; i < 5; i++ {
sum = sum + step
fmt.Println( step, i, sum );
time.Sleep(100 * time.Millisecond)
}
channel <- sum
}
func main() {
fmt.Println( "invoke Channel A" )
channelA()
fmt.Println( "invoke Channel B" )
channelB()
}
func channelA() {
channel := make( chan int )
go ccounter( 1, channel )
go ccounter( 2, channel )
x, y := <- channel, <- channel
fmt.Println( x, y )
}
func channelB() {
channel := make( chan int )
go ccounter( 3, channel )
x := <- channel
go ccounter( 4, channel )
y := <- channel
fmt.Println( x, y )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment