FAQ
I'm trying to write a simple function that can call any method on any
object. It looks like it should be easy, but I can't find a way. I've
looked at the "reflect" package but haven't been able to find what I need.
Can someone give me a clue?

package main

import (
"fmt"
)

type Person struct {}

func (p *Person) Run() {
fmt.Println("person running")
}

func doit(object interface{}, method interface{}) {
// how to invoke object.method() ?
}

func main() {
p := new(Person)
doit(p, (*Person).Run)
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Steven Blenkinsop at Feb 9, 2013 at 2:35 am
    A method expression just yields a function (there are no separate method
    types), so you can do this:
    http://play.golang.org/p/ZPyUi_4idh

    Obviously, "method" is now a misnomer, since it could be any function.

    On Fri, Feb 8, 2013 at 8:02 PM, wrote:

    I'm trying to write a simple function that can call any method on any
    object. It looks like it should be easy, but I can't find a way. I've
    looked at the "reflect" package but haven't been able to find what I need.
    Can someone give me a clue?

    package main

    import (
    "fmt"
    )

    type Person struct {}

    func (p *Person) Run() {
    fmt.Println("person running")
    }

    func doit(object interface{}, method interface{}) {
    // how to invoke object.method() ?
    }

    func main() {
    p := new(Person)
    doit(p, (*Person).Run)
    }

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Public Arena at Feb 9, 2013 at 2:57 am

    On Saturday, February 9, 2013 1:35:35 PM UTC+11, Steven Blenkinsop wrote:
    A method expression just yields a function (there are no separate method
    types), so you can do this:
    http://play.golang.org/p/ZPyUi_4idh

    Obviously, "method" is now a misnomer, since it could be any function.

    That's perfect, Steven, thank you!

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 9, '13 at 1:18a
activeFeb 9, '13 at 2:57a
posts3
users2
websitegolang.org

2 users in discussion

Public Arena: 2 posts Steven Blenkinsop: 1 post

People

Translate

site design / logo © 2022 Grokbase