如何在运行时在Go中获取函数参数,我只知道如何获取函数名称:

pc, file, line, ok := runtime.Caller(2)
rt := runtime.FuncForPC(pc)
return rt.Name() // Foo

我需要的是这样的:
Foo(1,2,3)
// Foo_1_2_3

最佳答案

没有完整的答案,但这也许可以帮助您:

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) {}

打印 :
func(int, string) int string

09-10 02:11
查看更多