Go through the following code samples, and predict what their output will be. No need to predict all the details of the stack traces; focus on determining which fmt.Println calls will get made, and in what order.
Solution
Example 1
Output:
a
panic: oh no
goroutine 1 [running]:
main.main()
/tmp/sandbox493149858/prog.go:7 +0xa0
Example 2
Output:
a
b
panic: oh no
goroutine 1 [running]:
main.myFunction()
/tmp/sandbox836036355/prog.go:7 +0xa0
main.main()
/tmp/sandbox836036355/prog.go:12 +0xa0
Example 3
Output:
a
panic: oh no
goroutine 1 [running]:
main.myFunction()
/tmp/sandbox950318916/prog.go:6 +0x40
main.main()
/tmp/sandbox950318916/prog.go:12 +0xa0
Example 4
Output:
a
c
oh no
b
Because the program recovered from the panic, it doesn’t exit early, and so no stack trace is printed.