{ "@context": "https://schema.org", "@type": "Article", "headline": "golang运行时包设置构建它的系统的文件路径", "datePublished": "2024-03-31T15:45:33", "dateModified": "2024-03-31T15:45:33", "description": "来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《golang运行时包设置构建它的系统的文件路径》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!问题内容我有一个简单的 go 代码,它使用 runtime 包,如下所示:package mainimport ( runtime fmt)func bar() { pc := make([]uintptr, 1000) n := runtime.ca", "publisher": { "@type": "Organization", "name": "Golang学习网", "url": "https://m.17golang.com" }, "mainEntityOfPage": { "@type": "WebPage", "@id": "https://m.17golang.com/article/118727.html" } }
登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-03-31 15:45:33 421浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《golang运行时包设置构建它的系统的文件路径》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我有一个简单的 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

似乎运行时包在编译期间设置了堆栈帧。

我需要根据gopath环境变量对文件路径进行一些处理,我已将其设置为a/home/usera /strong> 并作为机器 b 上的 /home/userb

我需要从每个文件路径中删除 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

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


解决方案


你想要实现什么目标? xy 问题询问您尝试的解决方案,而不是您的实际问题:The XY Problem

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运行时包设置构建它的系统的文件路径》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>