How to determine the method set of an interface in Golang?

前端 未结 2 867
花落未央
花落未央 2021-01-28 04:48

How would one print the method set of the following interface?

type Searcher interface {
    Search(query string) (found bool, err error)
    ListSearches() []st         


        
2条回答
  •  迷失自我
    2021-01-28 05:17

    Using reflection:

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

    Prints:

    main.Searcher
    ClearSearches
    ListSearches
    Search
    

提交回复
热议问题