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

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

Find the real caller in method call

问题

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

  1. type Parent struct {
  2. example string
  3. }
  4. func (p *Parent) GetMyAttr() {
  5. typ := reflect.TypeOf(p).Elem()
  6. for i := 0; i < typ.NumField(); i++ {
  7. p := typ.Field(i)
  8. if !p.Anonymous {
  9. fmt.Println(p.Name, ":", p.Type)
  10. }
  11. }
  12. }

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

  1. type Child struct {
  2. Parent
  3. another string
  4. }

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

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

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

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

英文:

I have a struct like this:

  1. type Parent struct {
  2. example string
  3. }
  4. func (p *Parent) GetMyAttr() {
  5. typ := reflect.TypeOf(p).Elem()
  6. for i := 0; i &lt; typ.NumField(); i++ {
  7. p := typ.Field(i)
  8. if !p.Anonymous {
  9. fmt.Println(p.Name, &quot;:&quot;, p.Type)
  10. }
  11. }
  12. }

And if I have another struct like this:

  1. type Child struct {
  2. Parent
  3. another string
  4. }

call GetTypeOfMe() in child like this

  1. ch := Child{Parent{&quot;example&quot;},&quot;another&quot;}
  2. 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):

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

将输出:

  1. Parent : main.Parent
  2. 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)

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

would output:

  1. Parent : main.Parent
  2. 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

  1. func GetAttr(i interface{}) {
  2. typ := reflect.Indirect(reflect.ValueOf(i)).Type()
  3. fmt.Println(typ)
  4. for i := 0; i < typ.NumField(); i++ {
  5. p := typ.Field(i)
  6. fmt.Println(p.Name, ":", p.Type)
  7. }
  8. }
英文:

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 :

  1. func GetAttr(i interface{}) {
  2. typ := reflect.Indirect(reflect.ValueOf(i)).Type()
  3. fmt.Println(typ)
  4. for i := 0; i &lt; typ.NumField(); i++ {
  5. p := typ.Field(i)
  6. fmt.Println(p.Name, &quot;:&quot;, p.Type)
  7. }
  8. }

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:

确定