We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
这一题的答案在这里:
https://github.com/mikespook/Learning-Go-zh-cn/blob/master/ex-channels/src/for-quit-chan.go
它的代码是这样的:
func main() { ch := make(chan int) quit := make(chan bool) go shower(ch, quit) for i := 0; i < 10; i++ { ch <- i } quit <- false // 或者是 true,这没啥关系 } func shower(c chan int, quit chan bool) { for { select { case j := <-c: fmt.Printf("%d\n", j) case <-quit: break } } }
main 程序在将false写入quitchannel后就退出了,此时是不是仍然有可能shower Goroutine 没有从quit中接收到值,整个程序已经结束了。
false
quit
shower
我感觉这个逻辑应该是show Goroutine 写入值到quitchannel中,然后main再等待结束吧。
show
package main import "fmt" func show(ch chan int, quit chan bool) { for { val, ok := <-ch if !ok { break } fmt.Println("Get the", val) } quit <- true } func main() { ch := make(chan int) quit := make(chan bool) go show(ch, quit) for i := 0; i < 10; i++ { ch <- i } close(ch) <-quit }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
这一题的答案在这里:
https://github.com/mikespook/Learning-Go-zh-cn/blob/master/ex-channels/src/for-quit-chan.go
它的代码是这样的:
main 程序在将
false
写入quit
channel后就退出了,此时是不是仍然有可能shower
Goroutine 没有从quit
中接收到值,整个程序已经结束了。我感觉这个逻辑应该是
show
Goroutine 写入值到quit
channel中,然后main再等待结束吧。The text was updated successfully, but these errors were encountered: