package main
func f(n int) (r int) {
a, r := n-1, n+1
if a+a == r {
c, r := n, n*n
r = r - c
}
return r
}
func main() {
println(f(3))
}
Choices:
Answer: 4
Run it on Go play.
Key point:
r
varialbe declared in the inner block is not a re-declaration of the one (the return result) declared in the function top block.
It is totally a new declared variable.
So the return result is not modified in the inner block.
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.