Back to questions
JuniorTheoryOccasional
What does the program print?
package main
import "fmt"
func count(n int) {
if n > 0 {
fmt.Print(n)
count(n - 1)
fmt.Print(n)
}
}
func main() { count(3) }321123
- Expecting
321: the line after the recursive call still runs, once that call returns, so eachcountprints itsntwice. - Expecting
123321or321321: the deepest call finishes first, so the second prints come back in reverse order: 1, 2, 3.
- What error or panic do you get when a Go recursion exhausts the stack?
- How does the goroutine stack growing on demand differ from a fixed C stack?