Go - sharing work - Goroutines and Channels - synchronization
Head First Go、 Jay McGavren(著)、O’Reilly Media)の Chapter 13(sharing work - Goroutines and Channels)、p.395(Exercise)の解答を求めてみる。
コード
main.go
package main
import "fmt"
func odd(channel chan int) {
channel <- 1
channel <- 3
}
func even(channel chan int) {
channel <- 2
channel <- 4
}
func main() {
channelA := make(chan int)
channelB := make(chan int)
go odd(channelA)
go even(channelB)
fmt.Println(<-channelA)
fmt.Println(<-channelB)
fmt.Println(<-channelA)
fmt.Println(<-channelB)
}
入出力結果(Terminal, Zsh)
% go run ./main.go
1
2
3
4
%