Skip to content

Instantly share code, notes, and snippets.

@sugilog sugilog/sample.go
Created Jan 24, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.