package main
var f = func(x int) {}
func Bar() {
f := func(x int) {
if x >= 0 {
print(x)
f(x - 1)
}
}
f(2)
}
func Foo() {
f = func(x int) {
if x >= 0 {
print(x)
f(x - 1)
}
}
f(2)
}
func main() {
Bar()
print(" | ")
Foo()
}
Choices:
Answer: 2 | 210
Run it on Go play.
Key points:
Bar
function, the second f
(the one in the call f(x - 1)
) is the package-level f
.
Foo
function, all three f
are the package-level f
.
Please read code blocks and identifier scopes for more detailed explanations.
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.