On Sun, Jul 3, 2022 at 1:37 PM Maxime FRYSOU <
maxpr...@gmail.com> wrote:
>
> I stumble upon this topic that is very interesting but I'm not 100% sure to fully understand what you meant when you said:
> "Checking for a struct field, embedded or not, can only be done using reflection.
> You can use a (much faster) type assertion to check for a method."
>
> I know it's been a while, but could you provide a piece of code to illustrate a bit just for clarity?
For example, you can test whether a type has a method "String()
string" by writing
_, hasStringMethod := v.(interface { String() string })
Ian
> On Thursday, 4 February 2016 at 23:41:40 UTC+1 Ian Lance Taylor wrote:
>>
>> On Thu, Feb 4, 2016 at 2:39 PM, Nuri Kevenoglu <
keve...@gmail.com> wrote:
>> >
>> > Is there a way to check whether a type contains a property by name and type
>> > without reflection e.g. contains property "id" (int64) or is reflection the
>> > only way?
>> >
>> > What about checking for the existence of an embedded type?
>>
>> Checking for a struct field, embedded or not, can only be done using reflection.
>>
>> You can use a (much faster) type assertion to check for a method.
>>
>> 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...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/2f93883c-d257-4732-beec-adabf2a77bd5n%40googlegroups.com.