FAQ
I want to return a function pointer from a method.

Example:

func (o *SomeStruct) dispatchCommand(cmd int) func() {
// return a method of SomeStruct based on the input
// I tried &o.SomeMethod, or o.SomeMethod - none of it worked
}

func (o *SomeStruct) someOtherMethod() {
f := o.dispatchCommand(7)
f()
}

How would I go about this in Go?
Is this possible?

--

Search Discussions

  • Miki Tebeka at Oct 29, 2012 at 11:39 pm
    You can't currently (AFAIK), see here<https://groups.google.com/forum/?fromgroups=#!searchin/golang-nuts/method$20pointer/golang-nuts/t1r6px7yGIY/ZrDORIcQmiAJ>for previous discussion.
    The best way is to create a closure and return it.
    On Monday, October 29, 2012 4:31:57 PM UTC-7, Erik Aigner wrote:

    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }

    func (o *SomeStruct) someOtherMethod() {
    f := o.dispatchCommand(7)
    f()
    }

    How would I go about this in Go?
    Is this possible?
    --
  • Erik Aigner at Oct 29, 2012 at 11:48 pm
    Thanks!
    On Tuesday, October 30, 2012 12:39:32 AM UTC+1, Miki Tebeka wrote:

    You can't currently (AFAIK), see here<https://groups.google.com/forum/?fromgroups=#!searchin/golang-nuts/method$20pointer/golang-nuts/t1r6px7yGIY/ZrDORIcQmiAJ>for previous discussion.
    The best way is to create a closure and return it.
    On Monday, October 29, 2012 4:31:57 PM UTC-7, Erik Aigner wrote:

    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }

    func (o *SomeStruct) someOtherMethod() {
    f := o.dispatchCommand(7)
    f()
    }

    How would I go about this in Go?
    Is this possible?
    --
  • Jesse McNelis at Oct 29, 2012 at 11:46 pm

    On Tue, Oct 30, 2012 at 10:31 AM, Erik Aigner wrote:
    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }
    You can do method expressions. eg.
    f := SomeStruct.SomeMethod
    http://golang.org/ref/spec#Method_expressions

    But that just gives you the method as a function that takes the
    receiver as it's first parameter.
    If you want to wrap up the receiver along with the function you'll
    need to make a closure.




    --
    =====================
    http://jessta.id.au

    --
  • Gofanster at Oct 30, 2012 at 1:20 am
    Would something like this work for you?

    package main

    import (
    "fmt"
    )

    type Dispatcher struct {
    SayHello func()
    SayGoodbye func()
    }

    func (d *Dispatcher) DispatchCommand(i int) func() {
    var f func()
    switch i {
    case 1:
    f = d.SayHello
    case 2:
    f = d.SayGoodbye
    }
    return f
    }

    func main() {
    d := Dispatcher{ SayHello: func() { fmt.Println("hello!") },
    SayGoodbye: func() { fmt.Println("goodbye!") }}
    d.DispatchCommand(1)()
    d.DispatchCommand(2)()
    }


    Output:

    hello!
    goodbye!
    On Monday, October 29, 2012 7:31:57 PM UTC-4, Erik Aigner wrote:

    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }

    func (o *SomeStruct) someOtherMethod() {
    f := o.dispatchCommand(7)
    f()
    }

    How would I go about this in Go?
    Is this possible?
    --
  • Erik Aigner at Nov 5, 2012 at 2:54 pm
    that assignment doesn't seem to work

    f = d.SayHello

    it says "method ... is not an expression, must be called"
    On Tuesday, October 30, 2012 2:20:55 AM UTC+1, gofa...@gmail.com wrote:

    Would something like this work for you?

    package main

    import (
    "fmt"
    )

    type Dispatcher struct {
    SayHello func()
    SayGoodbye func()
    }

    func (d *Dispatcher) DispatchCommand(i int) func() {
    var f func()
    switch i {
    case 1:
    f = d.SayHello
    case 2:
    f = d.SayGoodbye
    }
    return f
    }

    func main() {
    d := Dispatcher{ SayHello: func() { fmt.Println("hello!") },
    SayGoodbye: func() { fmt.Println("goodbye!") }}
    d.DispatchCommand(1)()
    d.DispatchCommand(2)()
    }


    Output:

    hello!
    goodbye!
    On Monday, October 29, 2012 7:31:57 PM UTC-4, Erik Aigner wrote:

    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }

    func (o *SomeStruct) someOtherMethod() {
    f := o.dispatchCommand(7)
    f()
    }

    How would I go about this in Go?
    Is this possible?
    --
  • Jan Mercl at Nov 5, 2012 at 3:32 pm

    On Mon, Nov 5, 2012 at 3:54 PM, Erik Aigner wrote:
    that assignment doesn't seem to work

    f = d.SayHello

    it says "method ... is not an expression, must be called"
    It works here: http://play.golang.org/p/8zj3MYUH8C

    -j

    --
  • Gofanster at Nov 5, 2012 at 6:12 pm
    from the compiler error you're reporting, it looks like you might have been
    doing / wanted to do something like this:

    /*
    Code doesn't compile.
    Compiler is complaining about the statements
    return d.SayHello
    return d.SayGoodbye
    in method dispatchCommand

    method d.SayHello is not an expression, must be called
    method d.SayGoodbye is not an expression, must be called
    */
    package main

    import (
    "fmt"
    )

    type Dispatcher struct {
    SomeField int
    }

    func (d *Dispatcher) SayHello() {
    fmt.Println("hello!")
    }

    func (d *Dispatcher) SayGoodbye() {
    fmt.Println("goodbye!")
    }

    func (d *Dispatcher) dispatchCommand(cmd int) func() {
    switch cmd {
    case 1:
    return d.SayHello
    case 2:
    return d.SayGoodbye
    }

    }

    func (d *Dispatcher) someOtherMethod() {
    f := d.dispatchCommand(d.SomeField)
    f()
    }


    func main() {
    d1 := &Dispatcher{1}
    d1.someOtherMethod()

    d2 := &Dispatcher{2}
    d2.someOtherMethod()

    }

    if you really want to go that route, you could use reflection.
    see my example in:
    http://play.golang.org/p/rd-iiSyuBW

    as you can see, the reflection approach adds complexity to your code,
    so it's not something i would recommend using unless i really have to.

    evan chua-yap



    On Monday, November 5, 2012 9:54:42 AM UTC-5, Erik Aigner wrote:

    that assignment doesn't seem to work

    f = d.SayHello

    it says "method ... is not an expression, must be called"
    On Tuesday, October 30, 2012 2:20:55 AM UTC+1, gofa...@gmail.com wrote:

    Would something like this work for you?

    package main

    import (
    "fmt"
    )

    type Dispatcher struct {
    SayHello func()
    SayGoodbye func()
    }

    func (d *Dispatcher) DispatchCommand(i int) func() {
    var f func()
    switch i {
    case 1:
    f = d.SayHello
    case 2:
    f = d.SayGoodbye
    }
    return f
    }

    func main() {
    d := Dispatcher{ SayHello: func() { fmt.Println("hello!") },
    SayGoodbye: func() { fmt.Println("goodbye!") }}
    d.DispatchCommand(1)()
    d.DispatchCommand(2)()
    }


    Output:

    hello!
    goodbye!
    On Monday, October 29, 2012 7:31:57 PM UTC-4, Erik Aigner wrote:

    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }

    func (o *SomeStruct) someOtherMethod() {
    f := o.dispatchCommand(7)
    f()
    }

    How would I go about this in Go?
    Is this possible?
    --
  • Francesc Campoy Flores at Nov 5, 2012 at 6:36 pm
    You don't need reflection for this:

    http://play.golang.org/p/R4ald_CDiD

    Cheers,
    Francesc
    On Mon, Nov 5, 2012 at 10:12 AM, wrote:

    from the compiler error you're reporting, it looks like you might have
    been doing / wanted to do something like this:

    /*
    Code doesn't compile.
    Compiler is complaining about the statements
    return d.SayHello
    return d.SayGoodbye
    in method dispatchCommand

    method d.SayHello is not an expression, must be called
    method d.SayGoodbye is not an expression, must be called
    */

    package main

    import (
    "fmt"
    )

    type Dispatcher struct {
    SomeField int
    }

    func (d *Dispatcher) SayHello() {
    fmt.Println("hello!")
    }

    func (d *Dispatcher) SayGoodbye() {
    fmt.Println("goodbye!")
    }

    func (d *Dispatcher) dispatchCommand(cmd int) func() {
    switch cmd {
    case 1:
    return d.SayHello
    case 2:
    return d.SayGoodbye
    }

    }

    func (d *Dispatcher) someOtherMethod() {
    f := d.dispatchCommand(d.SomeField)
    f()
    }


    func main() {
    d1 := &Dispatcher{1}
    d1.someOtherMethod()

    d2 := &Dispatcher{2}
    d2.someOtherMethod()

    }

    if you really want to go that route, you could use reflection.
    see my example in:
    http://play.golang.org/p/rd-iiSyuBW

    as you can see, the reflection approach adds complexity to your code,
    so it's not something i would recommend using unless i really have to.

    evan chua-yap




    On Monday, November 5, 2012 9:54:42 AM UTC-5, Erik Aigner wrote:

    that assignment doesn't seem to work

    f = d.SayHello

    it says "method ... is not an expression, must be called"
    On Tuesday, October 30, 2012 2:20:55 AM UTC+1, gofa...@gmail.com wrote:

    Would something like this work for you?

    package main

    import (
    "fmt"
    )

    type Dispatcher struct {
    SayHello func()
    SayGoodbye func()
    }

    func (d *Dispatcher) DispatchCommand(i int) func() {
    var f func()
    switch i {
    case 1:
    f = d.SayHello
    case 2:
    f = d.SayGoodbye
    }
    return f
    }

    func main() {
    d := Dispatcher{ SayHello: func() { fmt.Println("hello!") },
    SayGoodbye: func() { fmt.Println("goodbye!") }}
    d.DispatchCommand(1)()
    d.DispatchCommand(2)()
    }


    Output:

    hello!
    goodbye!
    On Monday, October 29, 2012 7:31:57 PM UTC-4, Erik Aigner wrote:

    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }

    func (o *SomeStruct) someOtherMethod() {
    f := o.dispatchCommand(7)
    f()
    }

    How would I go about this in Go?
    Is this possible?
    --


    --
    --
    Francesc

    --
  • Gofanster at Nov 5, 2012 at 7:30 pm
    Thanks Francesc! I did try return (*Dispatcher).SayHello and return
    (*Dispatcher).SayGoodbye but was getting compiler errors because I was
    using the wrong signature for the returned function (i found this out after
    looking at your code then mine again :-) )

    http://play.golang.org/p/Ny8sGyrbwN
    incorporates the necessary changes!

    evan chua-yap
    On Monday, November 5, 2012 1:36:39 PM UTC-5, Francesc Campoy Flores wrote:

    You don't need reflection for this:

    http://play.golang.org/p/R4ald_CDiD

    Cheers,
    Francesc
    On Mon, Nov 5, 2012 at 10:12 AM, <gofa...@gmail.com <javascript:>> wrote:

    from the compiler error you're reporting, it looks like you might have
    been doing / wanted to do something like this:

    /*
    Code doesn't compile.
    Compiler is complaining about the statements
    return d.SayHello
    return d.SayGoodbye
    in method dispatchCommand

    method d.SayHello is not an expression, must be called
    method d.SayGoodbye is not an expression, must be called
    */

    package main

    import (
    "fmt"
    )

    type Dispatcher struct {
    SomeField int
    }

    func (d *Dispatcher) SayHello() {
    fmt.Println("hello!")
    }

    func (d *Dispatcher) SayGoodbye() {
    fmt.Println("goodbye!")
    }

    func (d *Dispatcher) dispatchCommand(cmd int) func() {
    switch cmd {
    case 1:
    return d.SayHello
    case 2:
    return d.SayGoodbye
    }

    }

    func (d *Dispatcher) someOtherMethod() {
    f := d.dispatchCommand(d.SomeField)
    f()
    }


    func main() {
    d1 := &Dispatcher{1}
    d1.someOtherMethod()

    d2 := &Dispatcher{2}
    d2.someOtherMethod()

    }

    if you really want to go that route, you could use reflection.
    see my example in:
    http://play.golang.org/p/rd-iiSyuBW

    as you can see, the reflection approach adds complexity to your code,
    so it's not something i would recommend using unless i really have to.

    evan chua-yap




    On Monday, November 5, 2012 9:54:42 AM UTC-5, Erik Aigner wrote:

    that assignment doesn't seem to work

    f = d.SayHello

    it says "method ... is not an expression, must be called"
    On Tuesday, October 30, 2012 2:20:55 AM UTC+1, gofa...@gmail.com wrote:

    Would something like this work for you?

    package main

    import (
    "fmt"
    )

    type Dispatcher struct {
    SayHello func()
    SayGoodbye func()
    }

    func (d *Dispatcher) DispatchCommand(i int) func() {
    var f func()
    switch i {
    case 1:
    f = d.SayHello
    case 2:
    f = d.SayGoodbye
    }
    return f
    }

    func main() {
    d := Dispatcher{ SayHello: func() { fmt.Println("hello!") },
    SayGoodbye: func() { fmt.Println("goodbye!") }}
    d.DispatchCommand(1)()
    d.DispatchCommand(2)()
    }


    Output:

    hello!
    goodbye!
    On Monday, October 29, 2012 7:31:57 PM UTC-4, Erik Aigner wrote:

    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }

    func (o *SomeStruct) someOtherMethod() {
    f := o.dispatchCommand(7)
    f()
    }

    How would I go about this in Go?
    Is this possible?
    --


    --
    --
    Francesc
    --
  • Minux at Nov 5, 2012 at 6:27 pm

    On Mon, Nov 5, 2012 at 10:54 PM, Erik Aigner wrote:

    that assignment doesn't seem to work

    f = d.SayHello

    it says "method ... is not an expression, must be called"
    FYI: http://code.google.com/p/go/issues/detail?id=2280

    --
  • Tomwilde at Oct 30, 2012 at 9:12 am
    Like this?

    http://play.golang.org/p/kAzPKJBJpA

    Am Dienstag, 30. Oktober 2012 00:31:57 UTC+1 schrieb Erik Aigner:
    I want to return a function pointer from a method.

    Example:

    func (o *SomeStruct) dispatchCommand(cmd int) func() {
    // return a method of SomeStruct based on the input
    // I tried &o.SomeMethod, or o.SomeMethod - none of it worked
    }

    func (o *SomeStruct) someOtherMethod() {
    f := o.dispatchCommand(7)
    f()
    }

    How would I go about this in Go?
    Is this possible?
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 29, '12 at 11:32p
activeNov 5, '12 at 7:30p
posts12
users8
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase