Skip to content

Instantly share code, notes, and snippets.

@ksakae1216
Created July 4, 2018 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksakae1216/b403cc61f37027c64598f3e2438f767b to your computer and use it in GitHub Desktop.
Save ksakae1216/b403cc61f37027c64598f3e2438f767b to your computer and use it in GitHub Desktop.
package funcpkg
import (
"fmt"
"math/rand"
)
// SelectFunc ゴルーチンのサンプルです
func SelectFunc() {
ch1 := make(chan int)
ch2 := make(chan int)
ch0 := make(chan int)
fmt.Println("開始")
go func(c1 chan<- int, c2 chan<- int, c0 chan<- int) {
for {
// 0〜9のランダムな数値
randInt := rand.Intn(10)
if randInt == 0 {
ch0 <- randInt
break
} else if randInt%2 == 0 {
ch1 <- randInt
} else {
ch2 <- randInt
}
}
}(ch1, ch2, ch0)
for {
select {
case ch1Int := <-ch1:
fmt.Println("ch1受信 : ", ch1Int)
case ch2Int := <-ch2:
fmt.Println("ch2受信 : ", ch2Int)
case ch0Int := <-ch0:
fmt.Println("終了 : ", ch0Int)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment