如何在运行时在Go中获取函数参数,我只知道如何获取函数名称:
pc, file, line, ok := runtime.Caller(2)
rt := runtime.FuncForPC(pc)
return rt.Name() // Foo
Run Code Online (Sandbox Code Playgroud)
我需要的是这样的:
Foo(1,2,3)
// Foo_1_2_3
Run Code Online (Sandbox Code Playgroud)
不是完整的答案,但也许这可以帮助:
package main
import (
"fmt"
"reflect"
)
func main() {
fmt.Println(reflect.TypeOf(f1))
for index := 0; index < reflect.TypeOf(f1).NumIn(); index++ {
fmt.Println(reflect.TypeOf(f1).In(index))
}
}
func f1(a int, b string) {}
Run Code Online (Sandbox Code Playgroud)
印刷 :
func(int, string)
int
string
| 归档时间: |
|
| 查看次数: |
279 次 |
| 最近记录: |