#development #golang #pattern

Today, I wanted to add some timings to a function call in Go.

I wanted something very simple, so I opted for:

 1package main
 2
 3import (
 4    "fmt"
 5    "time"
 6)
 7
 8func main() {
 9
10    start := time.Now()
11    defer fmt.Println("ELAPSED:", time.Since(start))
12
13    time.Sleep(2 * time.Second)
14
15}

When running this, I was a bit surprised I got the following result:

$ go run defertester1.go
ELAPSED: 186ns

I was expecting to see an elapsed time of at least 2 seconds.

The caveat here is that as soon as the defer statement is called, it's already evaluating the result of fmt.Println("ELAPSED:", time.Since(start)).

After a search on StackOverflow, here's a nice description of why the result is like this:

The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns. For example, defer outcall(incall()), the incall() is evaluated immediately, while the outcall() is not executed until the surrounding function returns.

To get the correct result, you need to wrap this in a function call so that it only gets evaluated when the defer statement is executed.

 1package main
 2
 3import (
 4    "fmt"
 5    "time"
 6)
 7
 8func main() {
 9
10    start := time.Now()
11    defer func() {
12        fmt.Println("ELAPSED:", time.Since(start))
13    }()
14
15    time.Sleep(2 * time.Second)
16
17}

When we execute this, we now get the expected result:

$ go run defertester2.go
ELAPSED: 2.004822262s