英文:
Why does this golang function _not_ run forever?
问题
我想尝试FizzBuzz测试(为什么程序员不能编程),并使用Go语言。基本上是从1循环到100,当循环计数器可以被3整除时打印"Fizz",可以被5整除时打印"Buzz",同时可以被3和5整除时打印"FizzBuzz",否则只打印数字。
在使用迭代和递归的方法之后,我想使用并发(或使用通道)来完成。我想出了以下代码,令我惊讶的是它能正常工作:
func fizzbuzzconc() {
// 用于通信的通道
fizzchan := make(chan int)
buzzchan := make(chan int)
fizzbuzzchan := make(chan int)
nonechan := make(chan int)
// 启动Go协程来计算FizzBuzz挑战
go func() {
for i := 1; i <= 100; i++ {
if i % 3 == 0 && i % 5 == 0 {
fizzbuzzchan <- i
} else if i % 3 == 0 {
fizzchan <- i
} else if i % 5 == 0 {
buzzchan <- i
} else {
nonechan <- i
}
}
}()
// 这个for循环什么时候结束?
for {
select {
case i := <-fizzchan:
fmt.Println(i, "Fizz")
case i := <-buzzchan:
fmt.Println(i, "Buzz")
case i := <-fizzbuzzchan:
fmt.Println(i, "FizzBuzz")
case i := <-nonechan:
fmt.Println(i, i)
}
}
}
我无法理解这个for循环是如何结束的。它没有终止条件,也没有返回语句。为什么它最终会停止运行?
英文:
I wanted to try the FizzBuzz test (Why can't programmers program), and used Go. It's basically looping from 1 to 100, and printing "Fizz" when the loop counter is divisible by 3, "Buzz" when divisible by 5, "FizzBuzz" when divisible by both and else just print the number.
After doing it iteratively and recursively, I wanted to do it concurrently (or by using channels). I came up with the following code, which worked to my surprise:
func fizzbuzzconc() {
// Channels for communication
fizzchan := make(chan int)
buzzchan := make(chan int)
fizzbuzzchan := make(chan int)
nonechan := make(chan int)
// Start go routine to calculate fizzbuzz challenge
go func() {
for i := 1; i <= 100; i++ {
if i % 3 == 0 && i % 5 == 0 {
fizzbuzzchan <- i
} else if i % 3 == 0 {
fizzchan <- i
} else if i % 5 == 0 {
buzzchan <- i
} else {
nonechan <- i
}
}
}()
// When or how does this for loop end?
for {
select {
case i := <-fizzchan:
fmt.Println(i, "Fizz")
case i := <-buzzchan:
fmt.Println(i, "Buzz")
case i := <-fizzbuzzchan:
fmt.Println(i, "FizzBuzz")
case i := <-nonechan:
fmt.Println(i, i)
}
}
}
I can't understand how and why the for loop stops. There is no break condition, or a return statement. Why does it eventually finish running?
答案1
得分: 1
它并不真正有效。
经过一段时间后,由于剩余的 Go 协程在等待一个没有协程推送的通道,会发生逐渐减少的情况。所以你会遇到一个死锁(这是一个致命错误,会终止程序),而不是一个干净的结束。
总结起来,它“工作”是因为 Go 引擎足够智能,能够检测到死锁。
英文:
It doesn't really work well.
What happens, after a time, is that there's an attrition due to the remaining go-routine waiting for a channel where no goroutine pushes. So what you have is a dead-lock (which is a fatal error ending the program), not a clean ending.
In summary it "works" because the go engine is smart enough to detect the dead lock.
答案2
得分: 0
@dystroy已经很好地回答了你的问题,但是这里是你可能修复代码的方法。
一种整洁退出的方法是使用一个退出通道,我们通过关闭它来发出信号。(通过关闭通道发出信号很有用,因为可以有多个goroutine同时监听它)。
当然,还有其他的方法可以实现这个目的 - 如果你只有一个输出通道,那么你可以使用range
来读取结果并在完成后close
它。你可以很容易地重新编写代码以实现这种方式。
你可以使用sync.Waitgroup
来确保goroutine已经完成。
func main() {
// 用于通信的通道
fizzchan := make(chan int)
buzzchan := make(chan int)
fizzbuzzchan := make(chan int)
nonechan := make(chan int)
quit := make(chan struct{})
// 启动goroutine来计算fizzbuzz挑战
go func() {
for i := 1; i <= 100; i++ {
if i%3 == 0 && i%5 == 0 {
fizzbuzzchan <- i
} else if i%3 == 0 {
fizzchan <- i
} else if i%5 == 0 {
buzzchan <- i
} else {
nonechan <- i
}
}
close(quit)
}()
// 这个for循环什么时候结束?
OUTER:
for {
select {
case i := <-fizzchan:
fmt.Println(i, "Fizz")
case i := <-buzzchan:
fmt.Println(i, "Buzz")
case i := <-fizzbuzzchan:
fmt.Println(i, "FizzBuzz")
case i := <-nonechan:
fmt.Println(i, i)
case <-quit:
break OUTER
}
}
fmt.Println("All done")
}
英文:
@dystroy has answered your question very well, however here is how you might fix your code.
One way of exiting tidily is using a quit channel which we signal by closing it. (Signalling by closing a channel is useful because more than one go routine can be listening on it at once).
There are other ways of doing this though - if you only have one output channel then you range
over it to read the result and close
it when you finished. You could easily re-write this to work that way.
You can use a sync.Waitgroup
to make sure the go routine has finished also.
func main() {
// Channels for communication
fizzchan := make(chan int)
buzzchan := make(chan int)
fizzbuzzchan := make(chan int)
nonechan := make(chan int)
quit := make(chan struct{})
// Start go routine to calculate fizzbuzz challenge
go func() {
for i := 1; i <= 100; i++ {
if i%3 == 0 && i%5 == 0 {
fizzbuzzchan <- i
} else if i%3 == 0 {
fizzchan <- i
} else if i%5 == 0 {
buzzchan <- i
} else {
nonechan <- i
}
}
close(quit)
}()
// When or how does this for loop end?
OUTER:
for {
select {
case i := <-fizzchan:
fmt.Println(i, "Fizz")
case i := <-buzzchan:
fmt.Println(i, "Buzz")
case i := <-fizzbuzzchan:
fmt.Println(i, "FizzBuzz")
case i := <-nonechan:
fmt.Println(i, i)
case <-quit:
break OUTER
}
}
fmt.Println("All done")
}
答案3
得分: 0
@OneOfOne提到了sync.WaitGroup方法,我认为这是在Go中完成此任务的最佳方法。考虑到goroutine非常廉价,并且问题可以并行解决,我们可以为每个输入创建一个goroutine,并将结果发送到一个带缓冲的通道。
//要检查的大小
size := 100
results := make(chan Result, size)
//创建WaitGroup并设置latch大小。
var wg sync.WaitGroup
wg.Add(size)
//为每个并行操作创建一个goroutine
for i := 1; i <= size; i++ {
i := i //将值绑定到闭包中
go func() {
results <- fizzbuzz(i)
wg.Done() //释放一个latch
}()
}
//等待所有goroutine完成。
wg.Wait()
//关闭通道,以便我们可以退出下面的循环。
close(results)
//遍历结果,并在通道关闭后退出。
for x := range results {
fmt.Printf("i: %d result: %s\n", x.Nr, x.Val)
}
Playground代码:http://play.golang.org/p/80UafMax7M
英文:
@OneOfOne mentioned the sync.WaitGroup method which I think falls most in line with how you would do this in Go. Considering goroutines are very cheap and the problem can be solved in parallel, we can create a go routine for each input and send the results on a buffered channel.
//Size to check
size := 100
results := make(chan Result, size)
// Create the WaitGroup and set the latch size.
var wg sync.WaitGroup
wg.Add(size)
// Create a goroutine for each parallel operation
for i := 1; i <= size; i++ {
i := i //bind value into closure
go func() {
results <- fizzbuzz(i)
wg.Done() //release a latch
}()
}
//wait for all the goroutines to finish.
wg.Wait()
//close the channel so we can exit the below for loop.
close(results)
//Range over the results and exit once the channel has closed.
for x := range results {
fmt.Printf("i: %d result: %s\n", x.Nr, x.Val)
}
Playground code: http://play.golang.org/p/80UafMax7M
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论