Skip to content
New issue

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

Tour: Concurrency: Step 4: Channel should have not capacity #931

Open
rolandtritsch opened this issue Mar 28, 2020 · 1 comment
Open

Tour: Concurrency: Step 4: Channel should have not capacity #931

rolandtritsch opened this issue Mar 28, 2020 · 1 comment

Comments

@rolandtritsch
Copy link

Context: https://tour.golang.org/concurrency/4

Hi All,

I think in the context of what this step is suppose to illustrate creating the channel with the capacity of the number of fibs you want to calculate is miss-leading.

For this to work the channel needs to have (and should have) a capacity of one (basically synchronizing the fib goroutine with the main goroutine (between calculating one number and displaying it)).

func main() {
	c := make(chan int)
	go fibonacci(10, c)
	for i := range c {
		fmt.Println(i)
	}
}
@crisman
Copy link

crisman commented Feb 28, 2023

The code shows how range on a channel will continue until the channel is closed by the sending side. The capacity in the channel is there to allow fibonacci() to complete, sending all output over the channel, and close the channel if that (by scheduling chance) happens before the for loop starts reading from the channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants