如何在 reflectCallArgs 中返回 user.Class.Name
字符串。不可能吗?
type User struct {
Class struct {
Name string
}
}
var user = &User{}
reflectCallArgs := func(src interface{}) string {
//how to get this string?
return "user.Class.Name"
}
reflectCallArgs(user.Class.Name)
最佳答案
通过查询 runtime.Caller
,您可以获得的最接近的信息是调用函数的文件名和行号。 https://golang.org/pkg/runtime/#Caller
func Caller(skip int) (pc uintptr, file string, line int, ok bool)
Caller reports file and line number information about function invocations on the calling goroutine's stack. The argument skip is the number of stack frames to ascend, with 0 identifying the caller of Caller. (For historical reasons the meaning of skip differs between Caller and Callers.) The return values report the program counter, file name, and line number within the file of the corresponding call. The boolean ok is false if it was not possible to recover the information.
但是,您需要有权访问该文件并能够解析该文件(或那一行)以获取“调用者参数字符串”。
关于go - 如何在golang中获取调用者参数字符串?不可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51353793/