如何在Go中的调用堆栈上访问变量

前言:我理解这是一个值得商idea的想法,但是请想象一下我控制一个调用堆栈的开始和结束,但不控制某些中间函数的情况。这就是企业生活。

I'm looking to "recover" a context.Context that existed further up the callstack, but was omitted on its way down to the current runtime location. What would be a way to do this?

我走了多远? 我可以使用以下代码在调用堆栈中获取任意帧:

pc := make([]uintptr, 15)
n := runtime.Callers(2, pc) // skip the Callers frame, and this current frame
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()

which buys me access to a runtime Frame (source), but nothing else. Access to its internal _func may be useful, but here's where I'm stuck

评论