Parameters, multiple returns, variadic, closures, and defer.
Variadic Functions and Closures
Variadic — accept "any number of" arguments
A function whose last parameter is ...T accepts zero or more T values. Inside
the function, the parameter is a slice []T.
go playground
Loading...
The nums... syntax (suffix on the call site) is how you turn a slice back into
individual arguments. fmt.Println itself is variadic — that's why you can pass
it any number of values.
Closures — functions that capture variables
A closure is a function that references variables from the enclosing scope. The closure keeps those variables alive even after the outer function returns.
go playground
Loading...
Each call to counter() creates a fresh n. The returned function "captures" that
specific n and keeps mutating it.
`sum(nums ...int)` is variadic. You have `xs := []int{1, 2, 3}`. How do you pass them?