I just wrote the below code and could not understand why n2 is printed as 2. And my understanding is n2 should be printed as 0.
package main
import "fmt"
func curryAddTwo(n1 int) (n2 int) {
defer func() {
fmt.Println(n2) // why it prints 2?
} ()
return 2
}
func main() {
fmt.Println(curryAddTwo(1))
}
zhuangyan@C02D91SMMD6R ~/coding/go/closure $ ./main
2
2