Ok, asked and answered. Figuring out .Addr() was a key, as was finding the 
magic incantation to get a handle to a method. I'm curious if there's a 
better method.

https://play.golang.org/p/49Yj9G0egRI

package main

import (
"fmt"
"reflect"
)

type FooTypeA uint8
type FooTypeB uint8

type MyStruct struct {
Foo FooTypeA
Bar FooTypeB
}

func (mt *FooTypeA) Validate() bool {
return *mt > 1
}
func (mt *FooTypeB) Validate() bool {
return *mt > 0
}

func (ms *MyStruct) Validate() bool {
s := reflect.ValueOf(ms).Elem()

// Call Validate on all fields
for i := 0; i < s.NumField(); i++ {

f := s.Field(i).Addr()
method, _ := f.Type().MethodByName("Validate")
outs := method.Func.Call([]reflect.Value{f})
if !outs[0].Bool() {
return false
}
}
return true
}

func main() {

mytype := &MyStruct{
Foo: FooTypeA(2),
Bar: FooTypeB(5),
}

fmt.Println("what is mytype? ", mytype)

if !mytype.Validate() {
fmt.Println("Validation failed")
}
}

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