Re: [go-nuts] get interface and method name

2019-07-12 Thread Martin Schnabel

Because AccountService.Create is a method expression you can get to the
interface type by inspecting its first argument. Then you could check
the interface type for all methods and compare the input and output
parameters. This however is brittle and only works if you can guarantee
that there is no other method in the interface with the same parameter
lists.

So i would think using the runtime package is the only and therefor
best option for what you are asking.

Have fun!


On 12.07.19 11:06, Vasiliy Tolstov wrote:

чт, 11 июл. 2019 г. в 00:27, Ian Lance Taylor :


On Wed, Jul 10, 2019 at 1:40 PM Vasiliy Tolstov  wrote:


Hi! i have interface like

type AccountService interface {
   Create(context.Context) error
}

if i need to get string representation of this interface from passed
AccountService.Create how can i do that?

Now i create POC like this:
parts := 
strings.Split(runtime.FuncForPC(reflect.ValueOf(iface).Pointer()).Name(),
".")
return parts[len(parts)-2] + "." + parts[len(parts)-1]

it returns string "AccountService.Create"
Does it possible to get this not using runtime? only via reflect and
may be without strings ?


Can you show us a working example in the Go playground to demonstrated
what you are looking for?

If your interface is named AccountService, I would not expect any
method to be named AccountService.Create.  The Create method will be
defined on other types converted to the interface type, not on the
interface type itself.  So I'm not sure what you are actually looking
for.

Ian


https://play.golang.org/p/VZhO4KdKk-J
I need to return textual representation of interface name and method name.



--
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/1dbff307-443d-826c-8ecb-339107f7e573%40mb0.org.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] get interface and method name

2019-07-12 Thread Vasiliy Tolstov
чт, 11 июл. 2019 г. в 00:27, Ian Lance Taylor :
>
> On Wed, Jul 10, 2019 at 1:40 PM Vasiliy Tolstov  wrote:
> >
> > Hi! i have interface like
> >
> > type AccountService interface {
> >   Create(context.Context) error
> > }
> >
> > if i need to get string representation of this interface from passed
> > AccountService.Create how can i do that?
> >
> > Now i create POC like this:
> > parts := 
> > strings.Split(runtime.FuncForPC(reflect.ValueOf(iface).Pointer()).Name(),
> > ".")
> > return parts[len(parts)-2] + "." + parts[len(parts)-1]
> >
> > it returns string "AccountService.Create"
> > Does it possible to get this not using runtime? only via reflect and
> > may be without strings ?
>
> Can you show us a working example in the Go playground to demonstrated
> what you are looking for?
>
> If your interface is named AccountService, I would not expect any
> method to be named AccountService.Create.  The Create method will be
> defined on other types converted to the interface type, not on the
> interface type itself.  So I'm not sure what you are actually looking
> for.
>
> Ian

https://play.golang.org/p/VZhO4KdKk-J
I need to return textual representation of interface name and method name.

-- 
Vasiliy Tolstov,
e-mail: v.tols...@selfip.ru

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CACaajQs-Ag8Pxg9pdgNNev3M%2BAuTV-LPdM9%2B6Q4w2C8acjLgJQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] get interface and method name

2019-07-10 Thread Ian Lance Taylor
On Wed, Jul 10, 2019 at 1:40 PM Vasiliy Tolstov  wrote:
>
> Hi! i have interface like
>
> type AccountService interface {
>   Create(context.Context) error
> }
>
> if i need to get string representation of this interface from passed
> AccountService.Create how can i do that?
>
> Now i create POC like this:
> parts := 
> strings.Split(runtime.FuncForPC(reflect.ValueOf(iface).Pointer()).Name(),
> ".")
> return parts[len(parts)-2] + "." + parts[len(parts)-1]
>
> it returns string "AccountService.Create"
> Does it possible to get this not using runtime? only via reflect and
> may be without strings ?

Can you show us a working example in the Go playground to demonstrated
what you are looking for?

If your interface is named AccountService, I would not expect any
method to be named AccountService.Create.  The Create method will be
defined on other types converted to the interface type, not on the
interface type itself.  So I'm not sure what you are actually looking
for.

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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVZrcCMDpUgBZ2uEAnhFmKtE9_Zau5H1w_6CfzOBqnu3w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] get interface and method name

2019-07-10 Thread Vasiliy Tolstov
Hi! i have interface like

type AccountService interface {
  Create(context.Context) error
}

if i need to get string representation of this interface from passed
AccountService.Create how can i do that?

Now i create POC like this:
parts := 
strings.Split(runtime.FuncForPC(reflect.ValueOf(iface).Pointer()).Name(),
".")
return parts[len(parts)-2] + "." + parts[len(parts)-1]

it returns string "AccountService.Create"
Does it possible to get this not using runtime? only via reflect and
may be without strings ?

-- 
Vasiliy Tolstov,
e-mail: v.tols...@selfip.ru

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CACaajQt7QsQQSZ-tn2p8nyGL3XWdZm0ruTHu_-3-iL5P0gQOww%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.