go - 如何在golang中获取调用者参数字符串?不可能吗?

标签 go reflection runtime

如何在 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/

相关文章:

c# - 通过 COM 对象的反射获取属性名称

go - 调用win的api DnsQueryConfig,但它始终返回代码:87 ERROR_INVALID_PARAMETER

html - 如何在 gin gonic 框架(golang)中通过 c.HTML() 将函数传递给模板

c# - hangfire,RecurringJob AddOrUpdate反射调用c#

c# - 如何返回虚拟属性PropertyInfo?

java - 如何在运行时编译代码

sql - Golang连接到Cubrid

swift - http Post 说它可以工作,但它没有在本地主机上发布

c++ - main() 不执行,但编译

java - 关于 java : get `String[].class` from `String.class` , 如果 `String.class` 是 "runtime type"怎么办?