在研究 pkg/errors 时我很困惑。在文件stack.go中,我们可以看到关于结构体的注释Frame
如下:
// Frame represents a program counter inside a stack frame.
// For historical reasons if Frame is interpreted as a uintptr
// its value represents the program counter + 1.
type Frame uintptr
// pc returns the program counter for this frame;
// multiple frames may have the same PC value.
func (f Frame) pc() uintptr { return uintptr(f) - 1 }
谁能告诉我为什么 pc 函数得到uintptr(f) - 1
作为程序计数器的值。我写了一些关于pc的代码来测试,我可以得到正确的答案,尽管重写pc函数如下:
type Frame uintptr
func (f Frame) pc() uintptr { return uintptr(f) }
func (f Frame) line() int {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return 0
}
_, line := fn.FileLine(f.pc())
return line
}
func main() {
var pc, _, line, _ = runtime.Caller(0)
fmt.Printf("pc:%v, line:%v\n", pc, line)
fmt.Println("-------------")
frame := Frame(pc)
line = frame.line()
fmt.Printf("Frame:%v, line:%v\n", uintptr(frame), line)
}
代码的结果是:
pc:4779003, line:23
-------------
Frame:4779003, line:23