Last active
August 29, 2015 14:28
-
-
Save yuki2006/41c7386b46babd2bfd1d to your computer and use it in GitHub Desktop.
Goのchannelの送受信用の型について ref: http://qiita.com/yuki2006/items/3f90e53ce74c6cff1608
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" | |
) | |
func main() { | |
c := make(chan int, 10) | |
c2 := make(chan<- int, 10) | |
c2 = c | |
// c2に4を送信する | |
c2 <- 4 | |
// c2は送信専用なのでコンパイルエラー | |
// fmt.Println(<-c2) | |
// cからを受信すると4を受け取れる | |
fmt.Println(<-c) | |
} | |
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" | |
) | |
func fibonacci(n int, c chan<- int) { | |
x, y := 0, 1 | |
for i := 0; i < n; i++ { | |
c <- x | |
x, y = y, x+y | |
} | |
close(c) | |
} | |
func main() { | |
c := make(chan int, 10) | |
go fibonacci(cap(c), c) | |
for i := range c { | |
fmt.Println(i) | |
} | |
} | |
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
// 受信専用の容量10のchannel作成 | |
c := make(<-chan int, 10) | |
// 受信専用なのでコンパイルエラー | |
c <- 10 | |
fmt.Println(<-c) |
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
// 容量10のchannel作成 | |
c := make(chan int, 10) | |
// キューに送信 | |
c <- 10 | |
// キューから受信し、値を出力 | |
// キューに値がなければデッドロッグ | |
fmt.Println(<-c) //10 |
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" | |
) | |
func getPrimeGen() <-chan int { | |
var gen = make(chan int) | |
go func() { | |
var primes = make([]int, 0) | |
for k := 2; ; k++ { | |
check := true | |
for _, value := range primes { | |
if k % value == 0 { | |
check = false | |
break | |
} | |
} | |
if check { | |
primes = append(primes, k) | |
gen <- k | |
} | |
} | |
close(gen) | |
}() | |
return gen | |
} | |
func main() { | |
//受け取ったchannelは受信のみ | |
primeGen := getPrimeGen() | |
for i := 0; i < 10000-1; i++ { | |
<-primeGen | |
} | |
// 素数の10000番目を出力します。 | |
fmt.Println(<-primeGen) | |
} |
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
// 送信専用の容量10のchannel作成 | |
c := make(chan<- int, 10) | |
// キューに送信 | |
c <- 10 | |
// 送信専用なのでコンパイルエラーになる。 | |
fmt.Println(<-c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment