Pop quiz: what time was it?

The answer is a, the timestamps will be approximately the same (modulo the tiny amount of time between two sequential calls to time.Now() ). But why?

In Go, the arguments to any function are evaluated before the function is called. When applied to defer statements, the second time.Now() is evaluated immediately, then printed after the delay.

To cause the time.Now() evaluation to be deferred until the defer block runs, this syntax should be used

defer func() { fmt.Println(time.Now()) }()

This is a trivial example, but a more common occurrence of this bug can show up in logging and metrics collection, for example

func ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    fmt.Println("request started at", time.Now())
    defer fmt.Println("request completed at", time.Now())
    // handle request
}