Hi, all. I want to get receiver's embedded attribute in a method, for example:package mainimport ("fmt")type MyInt intfunc (i *MyInt)PrintEmbeddedAttr() {fmt.Println(EmbeddedAttr)}type Point struct {x, y MyInt}type NamedPoint struct {name stringPoint}func main() {var i MyIntvar p Pointvar np NamedPointi.PrintEmbeddedAttr() // nilp.x.PrintEmbeddedAttr() // method should output "Point.x"np.x.PrintEmbeddedAttr() // method should output "NamedPoint.Point.x"}How can I do it?
IMHO: You can't, not even with reflection.
I can print all field of a struct by using reflect package, but I can't find a way to find out whether the receiver is embedded in other struct or not in method.
Hi, all. I want to get receiver's embedded attribute in a method, for example:package mainimport ("fmt")type MyInt intfunc (i *MyInt)PrintEmbeddedAttr() {fmt.Println(EmbeddedAttr)}type Point struct {x, y MyInt}type NamedPoint struct {name stringPoint}func main() {var i MyIntvar p Pointvar np NamedPointi.PrintEmbeddedAttr() // nilp.x.PrintEmbeddedAttr() // method should output "Point.x"np.x.PrintEmbeddedAttr() // method should output "NamedPoint.Point.x"}How can I do it?
--
--
This only works from the outside, though. The original question was about doing if from inside the method, which is impossible. Also, this
won't navigate through embedded pointers, which means it only works half the time. If it's changed so it does, you can create infinite
Hi, all. I want to get receiver's embedded attribute in a method, for example:package mainimport ("fmt")type MyInt intfunc (i *MyInt)PrintEmbeddedAttr() {fmt.Println(EmbeddedAttr)}type Point struct {x, y MyInt}type NamedPoint struct {name stringPoint}func main() {var i MyIntvar p Pointvar np NamedPointi.PrintEmbeddedAttr() // nilp.x.PrintEmbeddedAttr() // method should output "Point.x"np.x.PrintEmbeddedAttr() // method should output "NamedPoint.Point.x"}How can I do it?
On Wed, Dec 19, 2012 at 12:41 AM, Steven Blenkinsop <stev...@gmail.com> wrote:This only works from the outside, though. The original question was about doing if from inside the method, which is impossible. Also, thiswhy? if a method want to know the receiver's embedded fields, you can use the same approach.
you just can't know if the receiver object is embedded within another object...
right, that's why I didn't trace the pointers, however, I think technically, i think embed a pointer to anotherstructure doesn't qualify as a embedded struct, i.e.
you can't say T2 embeds T, so I argue that my approach of not tracing the embedded pointers isactually correct.
for example, consider the fact you can embed a unsafe.Pointer in a struct, and technically, it canpoint to any type of object, so all types are embedded in this particular struct?
Thanks for everyone's enthusiasm. In simple terms, what I want is to get the container of an object:func ContainerOf(obj interface{}) interface{}