[Go] List a struct's methods with reflection

Created on November 30, 2026

Use reflect.TypeOf to get a reflect.Type for the value. Call NumMethod() to get the number of exported methods and Method(i).Name to get each name. The example passes a pointer to Hoge, so the method set includes its exported pointer-receiver methods.

package main

import (
    "fmt"
    "reflect"
)

type Hoge struct {
}

func (h *Hoge) First() string {
    return "one"
}

func (h *Hoge) Second() string {
    return "two"
}

func main() {
    h := &Hoge{}

    t := reflect.TypeOf(h)
    for i := 0; i < t.NumMethod(); i++ {
        fmt.Println(t.Method(i).Name)
    }
}

Output

First
Second
Go