在方法调用中找到真正的调用者

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

Find the real caller in method call

问题

我有一个像这样的结构体:

type Parent struct {
    example string
}

func (p *Parent) GetMyAttr() {
    typ := reflect.TypeOf(p).Elem()

    for i := 0; i < typ.NumField(); i++ {
        p := typ.Field(i)
        if !p.Anonymous {
            fmt.Println(p.Name, ":", p.Type)
        }
    }
}

如果我有另一个像这样的结构体:

type Child struct {
    Parent
    another string
}

在子结构体中调用GetTypeOfMe(),像这样:

ch := Child{Parent{"example"},"another"}
ch.GetMyAttr()

总是返回example : string。是否可能使用反射在父结构体中获取子结构体?

完整代码在这里:http://play.golang.org/p/ej4Xh_V2J1

英文:

I have a struct like this:

type Parent struct {
    example string
}
func (p *Parent) GetMyAttr() {
	typ := reflect.TypeOf(p).Elem()

	for i := 0; i &lt; typ.NumField(); i++ {
		p := typ.Field(i)
		if !p.Anonymous {
			fmt.Println(p.Name, &quot;:&quot;, p.Type)
		}
	}
}

And if I have another struct like this:

type Child struct {
	Parent
	another string
}

call GetTypeOfMe() in child like this

ch := Child{Parent{&quot;example&quot;},&quot;another&quot;}
ch.GetMyAttr()

is always return example : string .
Is this possible to get Child struct in Parent struct with reflection?

Full code is here http://play.golang.org/p/ej4Xh_V2J1

答案1

得分: 3

不,因为你操作的是*Parent,而不是*Child
这样会“起作用”(play.golang):

func (p *Child) GetMyAttr() {...}

将输出:

Parent : main.Parent
another : string

Parent不知道可能嵌入它的struct,所以反射只在Parent结构上操作。

你可以在“Go Reflection with Embedding”中看到类似的问题,它使用interface{}来获取传递的所有结构体。

英文:

No, since you operate on a *Parent, not a *Child.
This would "work" (play.golang)

func (p *Child) GetMyAttr() {...

would output:

Parent : main.Parent
another : string

A Parent doesn't know about the struct which might embed it, so the reflection operates only on Parent struct.

You see a similar issue with "Go Reflection with Embedding", which uses interface{} to get to all the struct passed in parameter.

答案2

得分: 3

这样是行不通的,reflect.TypeOf(p).Elem() 总是会返回 Parent。你有两个选择,要么为每个子类实现 GetMyAttr 方法,要么使用一个通用函数 like

func GetAttr(i interface{}) {
    typ := reflect.Indirect(reflect.ValueOf(i)).Type()
    fmt.Println(typ)
    for i := 0; i < typ.NumField(); i++ {
        p := typ.Field(i)
        fmt.Println(p.Name, ":", p.Type)
    }
}
英文:

It doesn't work like this, reflect.TypeOf(p).Elem() will always return Parent you have 2 options, either implement GetMyAttr for each child or use a generic function like :

func GetAttr(i interface{}) {
	typ := reflect.Indirect(reflect.ValueOf(i)).Type()
	fmt.Println(typ)
	for i := 0; i &lt; typ.NumField(); i++ {
		p := typ.Field(i)
		fmt.Println(p.Name, &quot;:&quot;, p.Type)

	}
}

huangapple
  • 本文由 发表于 2014年7月3日 18:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/24551681.html
匿名

发表评论

匿名网友

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

确定