gpt4 book ai didi

golang 运行时包从构建它的系统设置文件路径

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:28 24 4
gpt4 key购买 nike

我有一个简单的 go 代码,它使用 runtime 包如下:

package main

import (
"runtime"
"fmt"
)

func bar() {
pc := make([]uintptr, 1000)
n := runtime.Callers(0, pc)
frames := runtime.CallersFrames(pc[:n])
for {
frame, more := frames.Next()
if ! more {
break
}
fmt.Printf("FILE = %s and FUNC = %s\n", frame.File, frame.Function)
}
}

func foo() {
bar()
}

func main() {
foo()
}

我已经将 Go 二进制文件安装在 Ubuntu 机器(比如 机器 A)的自定义位置(/home/userA/bin/go)

我在机器A 上编译它并在同一台机器上运行可执行文件以获得输出:

FILE = /home/userA/bin/go/src/runtime/extern.go and FUNC = runtime.Callers
FILE = /home/userA/src/main.go and FUNC = main.bar
FILE = /home/userA/src/main.go and FUNC = main.foo
FILE = /home/userA/src/main.go and FUNC = main.main
FILE = /home/userA/bin/go/src/runtime/proc.go and FUNC = runtime.main

现在我将编译后的可执行文件复制到另一台 Ubuntu 机器(比如 机器 B),其中 Go 二进制文件安装在另一个不同的自定义位置(/home/userB/bin/走)。我从 /home/userB 运行了可执行文件。但这一次,我得到了与以前相同的输出:

FILE = /home/userA/bin/go/src/runtime/extern.go and FUNC = runtime.Callers
FILE = /home/userA/src/main.go and FUNC = main.bar
FILE = /home/userA/src/main.go and FUNC = main.foo
FILE = /home/userA/src/main.go and FUNC = main.main
FILE = /home/userA/bin/go/src/runtime/proc.go and FUNC = runtime.main

runtime 包似乎在编译期间设置了栈帧。

我需要根据 GOPATH 环境变量对文件路径做一些处理,我在机器 A< 上将其设置为 /home/userA/strong> 并作为 /home/userB 在机器 B 上。

我需要从每个文件路径中删除 GOPATH 部分。我用这个简单的函数调用来完成:strings.Replace(frame.File, GOPATH, "", 1)

但是,由于运行时包的这种行为,strings.Replace 函数无法替换机器 B 上文件路径的初始部分。

关于如何在机器 B 上完成这个有什么想法吗?

更新

按照@JimB 的建议,我用

构建了项目

CGO_ENABLED=0 go build -a -ldflags="-w -s"-gcflags=-trimpath=/home/userA -asmflags=-trimpath=/home/userA

现在,在同一台机器上运行可执行文件会得到如下输出:

FILE = /home/userA/bin/go/src/runtime/extern.go and FUNC = runtime.Callers
FILE = /home/userA/src/vendor/github.com/kataras/golog/golog.go and FUNC = vendor/github.com/kataras/golog.Error
FILE = /home/userA/src/test/test_logger.go and FUNC = test/test_logger.TestLogger
FILE = src/main.go and FUNC = main.bar
FILE = src/main.go and FUNC = main.foo
FILE = src/main.go and FUNC = main.main
FILE = /home/userA/bin/go/src/runtime/proc.go and FUNC = runtime.main

仅针对主文件修剪路径前缀。对于任何 vendor 导入的包或任何本地导入的包,它仍然存在。

最佳答案

你想完成什么? XY 问题是询问您尝试的解决方案而不是您的实际问题:The XY Problem .


Command go

The GOPATH environment variable lists places to look for Go code. On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. On Plan 9, the value is a list.


GOPATH 是 Go 工具的神器。它不在 Go 语言兼容性保证范围内。

如果 GOPATH 列表有多个元素会怎样?


这对你有用吗?

package main

import (
"fmt"
"runtime"
"strings"
)

func srcFile(path string) string {
const src = `/src/`
i := strings.LastIndex(path, src)
if i >= 0 {
path = path[i+len(src):]
}
return path
}

func bar() {
pc := make([]uintptr, 1000)
n := runtime.Callers(0, pc)
frames := runtime.CallersFrames(pc[:n])
for {
frame, more := frames.Next()
if !more {
break
}
fmt.Printf("PATH = %s and FUNC = %s\n", frame.File, frame.Function)
fmt.Printf("FILE = %s and FUNC = %s\n", srcFile(frame.File), frame.Function)
}
}

func foo() {
bar()
}

func main() {
foo()
}

输出:

PATH = /home/peter/go/src/runtime/extern.go and FUNC = runtime.Callers
FILE = runtime/extern.go and FUNC = runtime.Callers
PATH = /home/peter/gopath/src/so/gopath.go and FUNC = main.bar
FILE = so/gopath.go and FUNC = main.bar
PATH = /home/peter/gopath/src/so/gopath.go and FUNC = main.foo
FILE = so/gopath.go and FUNC = main.foo
PATH = /home/peter/gopath/src/so/gopath.go and FUNC = main.main
FILE = so/gopath.go and FUNC = main.main
PATH = /home/peter/go/src/runtime/proc.go and FUNC = runtime.main
FILE = runtime/proc.go and FUNC = runtime.main

关于golang 运行时包从构建它的系统设置文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51368837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com