Given the struct:
type A struct {
total int
}
and the func
(a *A) add func (i int, j int) (int x) {
a.total += i+j
return i+j
}
I would like to create a variable pointing to the func and invoke it via that variable. I'm having trouble defining the signature for the variable that includes the instance of the struct.
Why do this? Because I want to create an array of func pointers and iterate through it it calling them in sequence. I can have different arrays to accomplish different tasks using my func building blocks.
These two don't compile:
thisfunc func (a *A) (i int, b int) (x int)
thisfunc (a *A) func (i int, b int) (x int)
This one compiles but doesn't include the struct instance and when invoking thisfunc, a is missing - no struct instance, i.e null pointer dereference.
thisfunc func (i int, b int) (x int)
I would like to do something like this:
thisfunc = a.add
b := thisfunc(1,2)
Please help me define a variable thisfunc whose signature matches a.add.
thisfunccan't be a function that takes a struct pointer, rather than a method whose receiver is that struct pointer? e.g.func thisfunc(a *A, i, j int)