# Defer in Go

When it comes to keyword defer in go, I felt confused.

The teacher showed a piece of code:

func main() {
i := 1
defer fmt.Println(i + 1)

i++
fmt.Println("hello, world!")
}


I thought that it must print "hello, world!" and 3 at first. But after typing and runing, I was totally shocked —— It’s output is "hello, world" and 2!

What actually gets printed as a two, because that i +1 is evaluated right there when the first statement, when it hits the differed statement, the i +1 is evaluated and at that time, i is a one, so i +1 is a two. So, later when the differed statement actually gets executed it’s still prints a two.