执行不接受参数的方法

Given an arbitrary instance of a struct, I'd like to be able to execute all of its public methods which don't accept arguments.

For example in the code below, I'd like to be able to execute X{}.Foo() and X{}.Bar() without knowing that they exist.

package main

import (
    "fmt"
    "reflect"
)

type X struct {
    Y string
}

func (x X) Foo() string {
    return x.Y
}

func (x X) Bar() {
}

func (x X) Baz(q string) {
}

func main() {
    fooType := reflect.TypeOf(X{})
    for i := 0; i < fooType.NumMethod(); i++ {
        method := fooType.Method(i)
        fmt.Println(method.Name)
    }
}
  1. Change reflect.TypeOf to reflect.ValueOf
  2. Check the type of the Method to ensure that is accepts zero parameters
  3. Call the method with Call
foo := reflect.ValueOf(X{})
for i := 0; i < foo.NumMethod(); i++ {
    method := foo.Method(i)
    if method.Type().NumIn() == 0 {
        method.Call(nil)
    }
}

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