gpt4 book ai didi

go - 如何列出接口(interface)类型中的方法名称?

转载 作者:IT王子 更新时间:2023-10-29 01:44:41 26 4
gpt4 key购买 nike

例如,

type FooService interface {
Foo1(x int) int
Foo2(x string) string
}

我试图做的是使用运行时反射获取列表 ["Foo1", "Foo2"]

最佳答案

试试这个:

t := reflect.TypeOf((*FooService)(nil)).Elem()
var s []string
for i := 0; i < t.NumMethod(); i++ {
s = append(s, t.Method(i).Name)
}

playground example

获取接口(interface)类型的 reflect.Type 是棘手的部分。参见 How to get the reflect.Type of an interface?寻求解释。

关于go - 如何列出接口(interface)类型中的方法名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45071659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com