2 tricky behaviors of “defer”

When discovering Golang, “defer” seems at first a little exotic.
Here is 2 situations with no obvious behavior. …

Function calling a function in defer

If you want to time a function, at first sight, that may be a good idea to do :
package main
 
import (
	"fmt"
	"time"
)
 
func main() {
	start := time.Now()
	defer fmt.Println(time.Since(start))
 
	time.Sleep(100 * time.Millisecond)
}
and… wrong.…
Read more