消除go代码中的路径跟踪
时间:2024-03-21
浏览:315
作者:佚名
在 Go 编译过程中,尽管使用了 `-gcflags=-trimpath=$gopath` 和 `-asmflags=-trimpath=$gopath` 参数,但生成的跟踪信息中仍然保留了导入模块的绝对路径,导致程序异常。解决方法是使用 `-trimpath` 参数,而不是 `-gcflags` 或 `-asmflags`。该参数将从生成的执行文件中删除所有文件系统路径,并使用模块路径@版本或导入路径代替绝对路径。
问题内容
我需要删除跟踪中与导入的模块相对应的绝对路径。即使我这样编译我的程序: go build -gcflags=-trimpath=$gopath -asmflags=-trimpath=$gopath 我仍然得到模块文件的完整路径,其中出现了恐慌,尽管程序不显示完整路径:
goroutine 1 [running]:
monitors/fibre_monitor/logging.FileHandler(0x5e6755, 0x1a, 0x441, 0x0, 0x6fc23ac00, 0x1, 0x500000, 0xc000000002, 0xb)
/home/gtristan/go/src/monitors/fibre_monitor/logging/file_handler.go:182 +0x11f
main.python_logger(0x5e1383, 0x5, 0x5e6755, 0x1a, 0x101)
src/monitors/fibre_monitor/fibre_monitor.go:73 +0x1b5
main.main_check(0x0, 0xc00008e058)
src/monitors/fibre_monitor/fibre_monitor.go:343 +0x65
main.main()
src/monitors/fibre_monitor/fibre_monitor.go:428 +0x56
有什么办法可以摆脱跟踪中各处的 gopath?
解决方案
将 -trimpath 参数用于 go build(而不是 gcflags 或 asmflags):
没有 -trimpath:
$ go build .
$ ./panic
panic: bleh
goroutine 1 [running]:
main.example(0xc000046738, 0x2, 0x4, 0x473f2b, 0x5, 0xa)
/home/me/stuff/src/github.com/me/testing/panic/main.go:9 +0x39
main.main()
/home/me/stuff/src/github.com/me/testing/panic/main.go:4 +0x72
使用 -trimpath:
$ go build -trimpath .
$ ./panic
panic: bleh
goroutine 1 [running]:
main.example(0xc000046738, 0x2, 0x4, 0x473f2b, 0x5, 0xa)
github.com/me/testing/panic/main.go:9 +0x39
main.main()
github.com/me/testing/panic/main.go:4 +0x72
根据 go 帮助构建 :
-trimpath
remove all file system paths from the resulting executable.
Instead of absolute file system paths, the recorded file names
will begin with either "go" (for the standard library),
or a module path@version (when using modules),
or a plain import path (when using GOPATH).今天关于《消除go代码中的路径跟踪》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
