-1

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?

Manny Kayy
  • 33
  • 1
  • 6
  • "dynamically update the methods associated with these objects across a network" Who on God's green earth would want to maintain that code? If you want dynamic objects, use a dynamic language. – Seth Hoenig Feb 28 '14 at 14:20
  • Maintenance is not the issue in this case. This problem is similar to [this](http://stackoverflow.com/questions/16797191/how-do-i-create-an-array-of-method-pointers-in-c), except the array/slice of pointers should be a field of the struct. – Manny Kayy Feb 28 '14 at 14:35

2 Answers2

1

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)
        }
}
Seth Hoenig
  • 7,047
  • 6
  • 31
  • 30
  • 2
    I don't think the OP is solving his problem (which he hasn't explained well) in a idiomatic Go way; nonetheless, here is a syntax fixup of the code closer to the OPs original (than the above): http://play.golang.org/p/Wz1b203-CI – voidlogic Feb 28 '14 at 15:21
  • @voidlogic sorry but at the time, I didn't know how to describe it any better so i typed up some example code to show what I was trying to do. And your version is exactly what I have been trying to do. Thanks. – Manny Kayy Mar 01 '14 at 09:42
0

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.