- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有下面的代码,它根据运行的位置打印不同的程序计数器值。
代码:
package main
import (
"fmt"
"runtime"
)
func foo() {
bar()
}
func bar() {
pcs := make([]uintptr, 10)
_ = runtime.Callers(0, pcs)
for _, pc := range pcs {
fmt.Printf("Value of pc %+v\n", runtime.FuncForPC(pc).Name())
}
}
func main() {
foo()
}
go run
运行时或编译后的二进制文件,它会打印(main.bar
缺失)Value of pc runtime.Callers
Value of pc runtime.Callers
Value of pc main.main
Value of pc main.foo
Value of pc runtime.main
Value of pc runtime.goexit
Value of pc runtime.Callers
Value of pc main.bar
Value of pc main.foo
Value of pc main.main
Value of pc runtime.main
Value of pc runtime.goexit
foo
, bar
, 两者都不见了) Value of pc runtime.Callers
Value of pc runtime.Callers
Value of pc main.main
Value of pc main.main
Value of pc runtime.main
Value of pc runtime.goexit
我正在使用一个框架(logrus),它依赖于 PC 的顺序来执行一些操作(记录文件名)。由于 PC 值会根据其运行位置不断变化,因此它可以在 Debug模式下工作,但在使用
go run
运行时会失败或编译的二进制文件。
最佳答案
runtime.Callers()
的文档状态:
To translate these PCs into symbolic information such as function names and line numbers, use CallersFrames. CallersFrames accounts for inlined functions and adjusts the return program counters into call program counters. Iterating over the returned slice of PCs directly is discouraged, as is using FuncForPC on any of the returned PCs, since these cannot account for inlining or return program counter adjustment.
runtime.CallersFrames()
从知道并解释函数内联的原始计数器获取函数信息,例如:
pcs := make([]uintptr, 10)
n := runtime.Callers(0, pcs)
pcs = pcs[:n]
frames := runtime.CallersFrames(pcs)
for {
frame, more := frames.Next()
if !more {
break
}
fmt.Println("Function:", frame.Function)
}
无论您如何调用/运行它,这都应该输出(在
Go Playground 上尝试):
Function: runtime.Callers
Function: main.bar
Function: main.foo
Function: main.main
Function: runtime.main
关于go - runtime.Callers 根据运行的位置打印不同的程序计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64135896/
如果在主编辑器中突出显示功能名称,然后在助手编辑器中选择“调用者”,则可以查看突出显示的功能的调用位置。 助手编辑器顶部有向左和向右箭头,可让您跳至调用该函数的上一个/下一个位置。这些有键盘快捷键吗?
在 JupyterLab 上使用以下代码来运行 Google Video Intelligence Package: from google.cloud import videointelligenc
我有以下代码: Set b = ActiveSheet.Buttons(Application.Caller) With b.TopLeftCell bcn = .Column brn
我在一个子例程中使用 Application.Caller,我以编程方式将其绑定(bind)到我在工作表上找到的所有形状的 OnAction 属性。 Application.Caller 返回启动调用
我有下面的代码,它根据运行的位置打印不同的程序计数器值。 代码: package main import ( "fmt" "runtime" ) func foo() { ba
我在 VBA 中有以下代码: Sub Kontrollkästchen_KlickenSieAuf() With ThisWorkbook.Sheets("Hinterlegungsmatrix Au
通读 Effective Go 文档,我有点难以理解以下语句的含义。 If you pass a map to a function that changes the contents of the
我使用的是 Google Sheets API v4。 我已经用一些工作表进行了测试,我的代码可以正常工作,但有些则不行。我所做的只是换出工作表 ID。 我使用在这里找到的代码: https://de
两部分问题: 如何从对象方法获取function.caller。 如何使用该值来实际调用函数。 示例: foo = { bar: function() {console.log(bar.cal
批处理文件子例程的 return 语句等效于什么? 最佳答案 它是: goto :eof 对于习惯于普通编程语言的人来说,这是不寻常的,这是可以理解的。 我找到了这个信息 here . 关于windo
我想在与 WCF 服务通信时使用基于证书的加密和验证。所以我创建了测试证书,“TempCA”作为我的根 CA,“SignedByCA”作为由该 CA 签署的客户端证书。 当我将客户端证书放在“本地计算
我正在尝试以编程方式更新我的一个项目的结算帐户。我正在为 googleapis REST 使用 Node 客户端库。这是我的代码: const { google } = require('google
我最近注意到,在使用赋值的大多数情况下,重新初始化动态变量没有我预期的语义(但是,绑定(bind)以我预期的方式工作)。 具体来说,在这段代码中: sub g { my $*i = CALL
想象一下这种情况: Activity_1: get Activity_2 started with startActivityForResult; Activity_2: get Activity_3
间歇性地收到此错误 HttpError 403 when requesting https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest r
我还没有在一页中清楚地看到这一点:部分受信任的调用者。 我正在研究 APTCA,这总是被提及,但 MSDN 没有关于它的文章。 我只有一些线索,但我不是 100% 确定。 从网络共享执行的代码是否符合
我正在尝试从客户端控制台应用程序访问服务器上的 WCF 服务进行测试。我收到以下错误: The caller was not authenticated by the service 我正在使用wsH
当特定函数被调用两次时,我想看到 Angular 堆栈跟踪,这看起来很奇怪,但却是预期的行为 https://github.com/angular/angular.js/issues/1146 。 u
我有一些 jQuery 代码。我调用了一个 Ajax 函数文件 file.php,它有一些字段,例如: Milk. 我会再次分配给 jQuery 函数吗?如果是这样,我该怎么做?我附上了一个示例文件:
只是一些琐事;我在循环中创建 View 和模型,然后尝试将自定义事件绑定(bind)到它们。到目前为止一切都很好,但是当我想找出对象触发事件时呢?我知道还有其他冗长的方法可以将事件单独分配给系列中的每
我是一名优秀的程序员,十分优秀!