2

In Go, you can pass functions as parameters like callFunction(fn func). For example:

package main

import "fmt"

func example() {
    fmt.Println("hello from example")
}

func callFunction(fn func) {
    fn()
}    

func main() {
    callFunction(example)
}

But is it possible to call a function when it's a member of a struct? The following code would fail, but gives you an example of what I'm talking about:

package main

import "fmt"

type Example struct {
    x int
    y int
}

var example Example

func (e Example) StructFunction() {
    fmt.Println("hello from example")
}

func callFunction(fn func) {
    fn()
}    

func main() {
    callFunction(example.StructFunction)
}

(I know what I'm trying to do in that example is a little odd. The exact problem I have doesn't scale down to a simple example very well, but that's the essence of my problem. However I'm also intrigued about this from an academic perspective)

0

4 Answers 4

8

Methods (which are not "members of a struct" but methods of any named type, not only structs) are first class values. Go 1.0.3 didn't yet implemented method values but the tip version (as in the comming Go 1.1) has support method values. Quoting the full section here:

Method values

If the expression x has static type T and M is in the method set of type T, x.M is called a method value. The method value x.M is a function value that is callable with the same arguments as a method call of x.M. The expression x is evaluated and saved during the evaluation of the method value; the saved copy is then used as the receiver in any calls, which may be executed later.

The type T may be an interface or non-interface type.

As in the discussion of method expressions above, consider a struct type T with two methods, Mv, whose receiver is of type T, and Mp, whose receiver is of type *T.

type T struct {
    a int
}

func (tv  T) Mv(a int) int         { return 0 }  // value receiver
func (tp *T) Mp(f float32) float32 { return 1 }  // pointer receiver

var t T
var pt *T
func makeT() T

The expression

t.Mv

yields a function value of type

func(int) int

These two invocations are equivalent:

t.Mv(7)
f := t.Mv; f(7)

Similarly, the expression

pt.Mp

yields a function value of type

func(float32) float32

As with selectors, a reference to a non-interface method with a value receiver using a pointer will automatically dereference that pointer: pt.Mv is equivalent to (*pt).Mv.

As with method calls, a reference to a non-interface method with a pointer receiver using an addressable value will automatically take the address of that value: t.Mv is equivalent to (&t).Mv.

f := t.Mv; f(7)   // like t.Mv(7)
f := pt.Mp; f(7)  // like pt.Mp(7)
f := pt.Mv; f(7)  // like (*pt).Mv(7)
f := t.Mp; f(7)   // like (&t).Mp(7)
f := makeT().Mp   // invalid: result of makeT() is not addressable

Although the examples above use non-interface types, it is also legal to create a method value from a value of interface type.

var i interface { M(int) } = myVal
f := i.M; f(7)  // like i.M(7)
Sign up to request clarification or add additional context in comments.

Comments

3

Go 1.0 does not support the use of bound methods as function values. It will be supported in Go 1.1, but until then you can get similar behaviour through a closure. For example:

func main() {
    callFunction(func() { example.StructFunction() })
}

It isn't quite as convenient, since you end up duplicating the function prototype but should do the trick.

Comments

1

I fixed your compile errors.

package main

import "fmt"

type Example struct {
    x, y float64
}

var example Example

func (e Example) StructFunction() {
    fmt.Println("hello from example")
}

func callFunction(fn func()) {
    fn()
}

func main() {
    callFunction(example.StructFunction)
}

Output:

hello from example

3 Comments

I wasn't trying to compile that code, it was a mock up of my problem that I coded directly into SO. So th package/program and x/y type mistakes are just absent mindedness rather than the issue with my original code. I'm puzzled how you got output, because your code fails the same way mine did: method example.StructFunction is not an expression, must be called edit ahh the issue is the version of Go I'm running (1.0.3) doesn't support it. Tip does.
$ go version go version devel +1a196137ed09 Tue Apr 09 18:17:55 2013 +1000 linux/amd64 Does that mean that Go now does what you want?
Not yet, but when 1.1 pushes out to my repos then it will. Given it's only a couple of week around the corner, I'm happy to wait.
0

To add to @zzzz great answer (and the one given at https://golang.org/ref/spec#Method_values) here is an example that creates a method value from a value of an interface type.

package main

import "fmt"

type T struct{}

func (T) M(i int) { fmt.Println(i) }

func main() {
    myVal := T{}
    var i interface{ M(int) } = myVal
    f := i.M
    f(7) // like i.M(7)
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.