Go:方法的切片和结构体内的方法

huangapple go评论101阅读模式
英文:

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 (
&quot;fmt&quot;
)
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: &quot;micheal&quot;,
fooFunc: testFunc}
fooObject.fooFunc() 
fooObject = Foo{name: &quot;lisa&quot;,
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 &lt; 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(&quot;My name is &quot;, bar.name , &quot; and my age is &quot; , bar.age)
}
func (foo *Bar) barSalary() {
fmt.Println(&quot; My salary is &quot; , foo.salary)
}
func (foo *Bar) barDebt() {
fmt.Println(&quot; My salary is &quot; , 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 (
&quot;fmt&quot;
)
type FF func(*Foo)
type Foo struct {
foofunc FF
name string
age int
}
func foo1(f *Foo) {
fmt.Println(&quot;[foo1]&quot;, f.name)
}
func foo2(f *Foo) {
fmt.Println(&quot;[foo2] My name is &quot;, f.name , &quot; and my age is &quot; , f.age)
}
type BB func(*Bar)
type Bar struct {
barFuncs []BB
salary int
debt int
}
func barSalary(b *Bar) {
fmt.Println(&quot;[barSalary] My salary is &quot; , b.salary)
}
func barDebt(b *Bar) {
fmt.Println(&quot;[barDebt] My salary is &quot;, b.debt)
}
func main() {
fooObject := Foo{
name: &quot;micheal&quot;,
}
fooObject.foofunc = foo1
fooObject.foofunc(&amp;fooObject)
fooObject = Foo{
name: &quot;lisa&quot;,
age : 22,
}
fooObject.foofunc = foo2
fooObject.foofunc(&amp;fooObject)
barFuncList := make([]BB, 2, 2)
barFuncList[0] = barSalary
barFuncList[1] = barDebt
barObject := Bar{
salary: 45000,
debt: 200,
barFuncs: barFuncList,
}
for i := 0; i &lt; len(barObject.barFuncs); i++ {
barObject.barFuncs[i](&amp;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.

huangapple
  • 本文由 发表于 2014年2月28日 21:56:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/22097142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定