Created
July 4, 2018 05:57
-
-
Save ksakae1216/b403cc61f37027c64598f3e2438f767b to your computer and use it in GitHub Desktop.
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 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