On 01/18/2018 01:28 PM, Boone Severson wrote:
> Hi,
> 
> I've got structs containing custom types. I'm hoping to use reflect 
> to create some fairly generic code to Validate() every field of a
> struct based on its type, but I can't seem to get the code right. Is
> this possible? I've set up a playground to show where I've gotten.
> 
> https://play.golang.org/p/SgaGuLOjA4L
> 

The problem is that the Validate() functions take pointer receivers but
you MyStruct uses the actual FooTypeA and FooTypeB values.

You can get it to work by either making the Validate functions take a
copy of the value:

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

or by making the members of MyStruct pointers:

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

Check out the Method Sets section of the Go spec:

  https://golang.org/ref/spec#Method_sets

-ayan

-- 
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