Question

Is it possible to create a slice of methods or a slice of pointers to methods and store them in a field within a struct?

Below is is an example of the problem:

package main

import (
  "fmt"
)

type Foo struct {
  fooFunc func() /// Problem here
  name string
  age int
}

type Bar struct {
  barFunc []func() /// Problem here.
  salary int
  debt int
}

func main() {

  fooObject := Foo{name: "micheal",
             fooFunc: testFunc}

  fooObject.fooFunc() 

  fooObject = Foo{name: "lisa",
          age : 22,
          fooFunc: testFunc2}

  fooObject.fooFunc() 


  barFuncList := make([]func(), 2,2)
  barFuncList[0] = barSalary
  barFuncList[1] = barDebt

  barObject := Bar{name: fooObject.name,
           salary: 45000,
           debt: 200,
           barFunc: barFuncList)

  for i := 0; i < len(barObject.barFunc); i++{  // This is what I really want to do
    barObject.barFunc[i]
  }
}

func (bar *Foo) testfunc() {
  fmt.Println(bar.name)
}

func (bar *Foo) testfunc2() {
  fmt.Println("My name is ", bar.name , " and my age is " , bar.age)
}

func (foo *Bar) barSalary() {
  fmt.Println(" My salary is " , foo.salary)
}

func (foo *Bar) barDebt() {
  fmt.Println(" My salary is " , foo.debt)
}

Is there a way to attach methods of an object to the field of its struct?

Is it also possible to put a slice of the object's methods in a field of its struct?

Was it helpful?

Solution

Go isn't capable of monkey patching (hooray!), but you can do dynamic function calls from an object method if you really want to. I modified (and fixed) your code just a bit to show this.

http://play.golang.org/p/2rwCW2N93-

package main

import (
  "fmt"
)

type FF func(*Foo)

type Foo struct {
  foofunc FF
  name string
  age int
}

func foo1(f *Foo) {
        fmt.Println("[foo1]", f.name)
}

func foo2(f *Foo) {
        fmt.Println("[foo2] My name is ", f.name , " and my age is " , f.age)
}


type BB func(*Bar)

type Bar struct {
  barFuncs []BB
  salary int
  debt int
}

func barSalary(b *Bar) {
        fmt.Println("[barSalary] My salary is " , b.salary)
}

func barDebt(b *Bar) {
        fmt.Println("[barDebt] My salary is ", b.debt)
}

func main() {

        fooObject := Foo{
                name: "micheal",
        }
        fooObject.foofunc = foo1

        fooObject.foofunc(&fooObject)

        fooObject = Foo{
                name: "lisa",
                age : 22,
        }
        fooObject.foofunc = foo2

        fooObject.foofunc(&fooObject)



        barFuncList := make([]BB, 2, 2)
        barFuncList[0] = barSalary
        barFuncList[1] = barDebt

        barObject := Bar{
                salary: 45000,
                debt: 200,
                barFuncs: barFuncList,
        }

        for i := 0; i < len(barObject.barFuncs); i++ {
                barObject.barFuncs[i](&barObject)
        }
}

OTHER TIPS

Well i don't think you can dynamically update methods on a field for a struct but it is possible to dynamically dispatch methods on interfaces.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top