Goroutines:Channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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