Panic Stack

What will be printed when the code below is executed?

  1. package main
  2. func f1() {
  3. defer println("f1-begin")
  4. f2()
  5. defer println("f1-end")
  6. }
  7. func f2() {
  8. defer println("f2-begin")
  9. f3()
  10. defer println("f2-end")
  11. }
  12. func f3() {
  13. defer println("f3-begin")
  14. panic(0)
  15. defer println("f3-end")
  16. }
  17. func main() {
  18. f1()
  19. }

Answer

  1. f3-begin
  2. f2-begin
  3. f1-begin
  4. panic: 0
  5. goroutine 1 [running]:
  6. main.f3()
  7. /tmp/sandbox423933384/main.go:17 +0x80
  8. main.f2()
  9. /tmp/sandbox423933384/main.go:11 +0x80
  10. main.f1()
  11. /tmp/sandbox423933384/main.go:5 +0x80
  12. main.main()
  13. /tmp/sandbox423933384/main.go:22 +0x20