这个Fibonacci代码如何使用通道和Go协程运行?

huangapple go评论81阅读模式
英文:

How does this Fibonacci code run with channels and go routines?

问题

有一个名为rosetta code的网站,其中包含各种语言的算法,因此您可以在学习新语言时进行学习和比较。

在这里,我看到Go语言的一个解决方案非常有趣,但我不完全理解它。

func fib(c chan int) {
	a, b := 0, 1
	for {
		c <- a
		a, b = b, a+b
	}
}

func main() {
	c := make(chan int)
	go fib(c)
	for i := 0; i < 10; i++ {
		fmt.Println(<-c)
	}
}

以下是我对此的一些疑问:

无限for循环如何知道何时停止?
c通道如何传达这一点?
func调用之间的逻辑顺序是什么?

感谢善良的陌生人的帮助。

英文:

There is this website called rosetta code that has algorithms in all languages so you can learn and compare when learning new languages.

Here I saw that one of the solutions for go lang is pretty interesting but I don't fully understand it.

func fib(c chan int) {
	a, b := 0, 1
	for {
		c &lt;- a
		a, b = b, a+b
	}
}

func main() {
	c := make(chan int)
	go fib(c)
	for i := 0; i &lt; 10; i++ {
		fmt.Println(&lt;-c)
	}
}

Here are some of my doubts

How does the infinite for loop know when to stop?
How does the c channel communicate this?
What is the logical sequence between the func calls?

Thanks for the help kind strangers.

答案1

得分: 3

无限循环如何知道何时停止?

正如你所说:这是一个无限循环,只要程序在运行,它就不会停止。

c通道如何传达这一点?

c通道根本不会传达停止for循环的信息,循环不会停止。c的唯一目的是将计算站点(无限for循环)中的下一个数字传递到使用站点(打印循环)。

func调用之间的逻辑顺序是什么?

go fib(c)将fib作为goroutine启动。这是您的代码中唯一发生的函数调用(*)。一旦发生了go fib(c),您就有两个并发的事情在运行:1.主函数将打印10次,2. fib(c)执行计算。

有趣的部分——main()和fib(c)之间的同步——发生在main执行<-c和(“在同一时刻”)fib执行c <- a时。当main和fib都达到这些行时,两者将“同时发生”:fib将写入/发送到c,main从c“同时”消费/接收。之后,main和fib函数都会独立继续执行。

一旦main完成,程序就会结束(这也会“停止”fib的无限循环)。

(*)对于吹毛求疵的人:除了对于理解此代码无关紧要的fmt.Printf和make调用。

英文:

> How does the infinite for loop know when to stop?

As you said: This is an infinite loop and doesn't stop at all (as long as the program is running).

> How does the c channel communicate this?

The channel c doesn't communicate stopping the for loop at all, the loop is not stopped. The sole purpose of c is to deliver the next number in the sequence from the calculation site (the infinite for loop) to the usage site (the print loop).

> What is the logical sequence between the func calls?

go fib(c) starts fib as a goroutine. This is the one and only function call (*) ever happening in your code. Once go fib(c) has happened you have to concurrent things running: 1. The main function which will print 10 times and 2. fib(c) which does the computation.

The interesting stuff -- the synchronization between main() and fib(c) -- happens when main executes &lt;-c and ("in the same moment") fib executes c &lt;- a. Both functions, main and fib work until they both reach those lines. Once both are "at that line", both will happen "at the same time": fib will write/send to c and main consumes/receives from c "simultaneous". Afterwards both functions main and fib continue independently.

Once main is done the program finishes (this also "stops" fib's infinite loop).

(*) for the nitpickers: besides the fmt.Printf and make call which are irrelevant for understanding of this code.

huangapple
  • 本文由 发表于 2022年9月14日 12:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/73711667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定