Edit:  It looks like this has more to do with being an interface
method than an embedded type.

https://play.golang.org/p/I5XPdWR_O0

Is there a generic way to get the caller safely, or should I just
check for <autogenerated> and increment?

On Sun, Aug 14, 2016 at 3:02 PM, Tim Hockin <thoc...@google.com> wrote:
> It is, in fact, a method on an embedded type.  That means I can document it
> and it ceases to be a magic number!  Thanks.
>
>
> On Aug 14, 2016 12:27 PM, "Ian Lance Taylor" <i...@golang.org> wrote:
>>
>> On Sun, Aug 14, 2016 at 11:10 AM, 'Tim Hockin' via golang-nuts
>> <golang-nuts@googlegroups.com> wrote:
>> > I was playing with a method that wants to print the file:line of the
>> > caller.  `runtime.Caller(1)` seems like it should do the trick, but I
>> > get '<autogenerated>:2'.  If I call `runtime.Caller(2)` instead it
>> > seems to work, but I don't know why, so I am reluctant to depend on
>> > that.
>> >
>> > Can anyone enlighten me?  How can I know how many frames to crawl back
>> > to my real caller?
>> >
>> > go1.6.1 on linux/amd64
>>
>> An `autogenerated` frame means that you are calling a method that is
>> defined on some type T1 that is embedded in some other type T2.  The
>> `autogenerated` code is the little wrapper that takes a value of type
>> T2 and invokes the method on the embedded field of type T1.  As far as
>> I know you will never see autogenerated code called by autogenerated
>> code.  So you only need to worry about this in a method of a type that
>> your program will embed into some other type.  If you don't know at
>> run time whether the type is embedded or not, call runtime.Caller(1),
>> and if you see autogenerated call runtime.Caller(2).
>>
>> Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to