Passing a function https://play.golang.org/p/6QAnd--O_hP


(Cherolyn Lexvold) #1

https://play.golang.org/p/6QAnd--O_hP

Which function is passed here?


(Norbert Melzer) #2

In this line:

	t := evenSum(sum, []int{1, 2, 3, 4, 5, 6, 7, 8, 9}...)

You are passing the function sum to the function evenSum.

In evenSum then it is bound to f via the functions argument (f func(x ...int) int makes f accept any function that is variadic and takes only ints).


(Cherolyn Lexvold) #3

Thanks. I think that answers another question that I had, but I didn’t know how to phrase. The question was , in “func sum(x …int) int {”, whether “sum” is a random name for the function , or is it predetermined


(Norbert Melzer) #4

It’s freely choose (according to the rules of allowed identifier) by the developer.


(Cherolyn Lexvold) #5

This is very interesting to me


(Norbert Melzer) #6

What is “this”? Naming stuff or passing around functions?