Last active
August 29, 2015 14:00
-
-
Save krrrr38/5e9dce25e41e41ee57b9 to your computer and use it in GitHub Desktop.
goroutineに出来ないのなんで
This file contains hidden or 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 rec(depth int, ch chan int) { | |
if depth > 3 { | |
return | |
} else { | |
ch <- depth | |
rec(depth+1, ch) // cannot be gorouting | |
} | |
} | |
func main() { | |
ch := make(chan int) | |
go func() { | |
rec(0, ch) | |
close(ch) | |
}() | |
for i := range ch { | |
fmt.Printf("value = %v\n", i) | |
} | |
} |
再帰でgoroutineにした場合に、for i:= range ch
のとこでdeadlockしないようにcloseさせる上手いやり方?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
goroutineにしたら17行目通過してcloseしちゃうからだ