Sobes.tech
Junior — Senior

How does defer work inside a for loop

livecode

Task condition

It is necessary to predict what text will be output to the console when the defer operator is applied to delay the output of a variable within the loop body.

func main() {
    fmt.Println("start")

    for i := 1; i < 4; i++ {
        defer fmt.Println(i)
    }

    fmt.Println("end")
}