Sobes.tech
Junior — Senior

Understanding the order of deferred calls execution

livecode

Task condition

You need to determine what result the program will output. Understand how defer statements work in Go and whether their order affects the return value of the function.

package main

import (
 "fmt"
 "math"
)

func foo() (k float64) {
 i := 36.0
 defer func() { k = math.Sqrt(k) }()
 defer func() { k -= 11 }()
 defer func() { i += 10 }()
 return 1
}

func main() {
 fmt.Println(foo())
}