Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
op := func(a, b int) int { return a + b }
r := op(2, 3)
op = func(a, b int) int { return a * b }
fmt.Println(r, op(2, 3))
}5 6
- Expecting
6 6:r := op(2, 3)calls the function at once and stores the number 5, not the function. - Expecting
5 5or a compile error: a function is a value, so afunc(int, int) intvariable takes another literal of that type.
- How does an anonymous function become a closure over surrounding variables?
- What is the zero value of a function-typed variable, and what happens if you call it?