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?