Single answer

What does this program print when you run it with go run main.go?

package main

import "fmt"

func main() {
    for i := 0; i < 3; i++ {
        defer fmt.Print(i, " ")
    }
    fmt.Print("start ")
}
Single answer

What does this program print?

package main

import "fmt"

func main() {
    x := 1
    defer fmt.Println("deferred:", x)
    x = 2
    fmt.Println("now:", x)
}
Single answer

This version defers a closure instead of a direct call. What does it print?

package main

import "fmt"

func main() {
    x := 1
    defer func() {
        fmt.Println("deferred:", x)
    }()
    x = 2
    fmt.Println("now:", x)
}
Single answer

Two functions double their result in a deferred closure. What does this program print?

package main

import "fmt"

func named() (n int) {
    defer func() {
        n *= 2
    }()
    return 5
}

func unnamed() int {
    n := 5
    defer func() {
        n *= 2
    }()
    return n
}

func main() {
    fmt.Println(named(), unnamed())
}
Single answer

A division helper turns a panic into an error. What does this program print?

package main

import "fmt"

func safeDivide(a, b int) (q int, err error) {
    defer func() {
        if r := recover(); r != nil {
            err = fmt.Errorf("recovered: %v", r)
        }
    }()
    q = a / b
    return q, nil
}

func main() {
    fmt.Println(safeDivide(10, 0))
}
Single answer

What happens when this program runs?

package main

import "fmt"

func main() {
    if r := recover(); r != nil {
        fmt.Println("caught:", r)
    }
    panic("disk full")
}
Single answer

main has a deferred recover, and a goroutine panics. What happens when this program runs?

package main

import "fmt"

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("main recovered:", r)
        }
    }()
    done := make(chan struct{})
    go func() {
        panic("worker failed")
    }()
    <-done
    fmt.Println("after worker")
}
Single answer

What does this program print before it exits?

package main

import "fmt"

func main() {
    defer fmt.Println("closing file")
    fmt.Println("writing")
    panic("disk full")
}
Multiple answers

Which statements about this program are true? Select all that apply.

package main

import (
    "fmt"
    "os"
)

func main() {
    defer fmt.Println("deferred in main")
    for i := 1; i <= 2; i++ {
        defer fmt.Println("deferred in iteration", i)
        fmt.Println("iteration", i, "ends")
    }
    fmt.Println("recover with no panic:", recover())
    os.Exit(3)
}
Single answer

True or false: after a deferred function recovers from a panic, execution continues on the line right after the statement that panicked.

package main

import "fmt"

func process() (status string) {
    defer func() {
        if r := recover(); r != nil {
            status = "recovered"
        }
    }()
    status = "started"
    panic("bad record")
    status = "finished"
    return status
}

func main() {
    fmt.Println(process())
    fmt.Println("main continues")
}