英文:
Go: slice of methods and methods within structs
问题
在Go语言中,是不允许将方法直接存储在结构体的字段中的。但是,你可以将方法的指针存储在结构体的字段中,并通过指针调用方法。
以下是修改后的代码示例:
package main
import (
"fmt"
)
type Foo struct {
fooFunc func() // 存储方法指针
name string
age int
}
type Bar struct {
barFunc []*func() // 存储方法指针的切片
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++ {
(*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 barSalary() {
fmt.Println("My salary is", 45000)
}
func barDebt() {
fmt.Println("My debt is", 200)
}
这样,你就可以将方法的指针存储在结构体的字段中,并通过指针调用方法。同时,你也可以将方法的指针存储在结构体字段的切片中。
英文:
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?
答案1
得分: 1
Go语言不支持猴子补丁(太好了!),但是如果你真的想要,你可以从对象方法中进行动态函数调用。我稍微修改了你的代码来展示这一点。
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] 我的名字是", f.name, ",我的年龄是", f.age)
}
type BB func(*Bar)
type Bar struct {
barFuncs []BB
salary int
debt int
}
func barSalary(b *Bar) {
fmt.Println("[barSalary] 我的薪水是", b.salary)
}
func barDebt(b *Bar) {
fmt.Println("[barDebt] 我的债务是", 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)
}
}
你可以在这里查看修改后的代码:链接
英文:
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)
}
}
答案2
得分: 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论