0

I thought I understood how passing a method as an argument works, clearly I don't. I am trying to get the outer function to call a method on the receiver which I declared explicitly. Here is my code;

package main

type Foo struct {
}

func (f Foo) sum(a, b int) int {
    return a + b
}

func main() {
    var foo Foo
    bar := func(fn func(f Foo)) {
        fn(foo)
    }
    bar(Foo.sum(foo, 2, 2))
}

Error

cannot use Foo.sum(foo, 2, 2) (value of type int) as func(f Foo) value in argument to bar

I am doing something wrong but I just can't figure it out.

I tried adding an int return type but I am still getting the error. I expect the func on call on the method with "foo" as the receiver and the two ints to get added.

Dassy Areg
  • 15
  • 8

0 Answers0