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?