为了账号安全,请及时绑定邮箱和手机立即绑定

配置 [go build]/[go test] 等以在诊断消息中显示绝对路径

配置 [go build]/[go test] 等以在诊断消息中显示绝对路径

Go
繁星点点滴滴 2023-07-31 16:06:50
有没有办法配置/直接go build等go test来使用绝对路径报告文件位置?我经常go build从编辑器(解析输出)运行,如果进程的当前工作目录go build和编辑器本身不需要匹配,那就很方便了。到目前为止,我找到的唯一解决方案是封装go build一个确定文件绝对路径的脚本,然后在使用'd 原始参数cd调用之前立即转到临时目录。go buildrealpath创建一个临时目录并cd访问它只是为了欺骗 go 工具似乎是一个奇怪的解决方法,所以我想知道是否有更直接的解决方案。go tools除了包装绝对路径并cd随机目录之外,还有其他方法可以配置报告绝对路径吗?假设我的GOPATH是/go并且我有以下文件。// /go/src/nonexistent-website.com/example-2019-10-26/main.gopackage mainimport (    "fmt")func main() {    fmt.Println("hi")}假设我故意在这个文件中引入一个错误。// /go/src/nonexistent-website.com/example-2019-10-26/main.gopackage main// import (//  "fmt"// )func main() {    fmt.Println("hi")}go build main.go报告以下错误:/go/src/nonexistent-website.com/example-2019-10-26$ go build main.go# command-line-arguments./main.go:9: undefined: fmt in fmt.Println   2go build ...如果给定绝对路径,仍然报告./main.go为文件的路径/go/src/nonexistent-website.com/example-2019-10-26$ go build `realpath main.go`# command-line-arguments./main.go:9: undefined: fmt in fmt.Println   2go build似乎生成了相对于调用它的目录的路径:go build这些是首先定向到cd根目录时生成的路径。/go/src/nonexistent-website.com/example-2019-10-26$ (a=`realpath main.go` && cd / && go build $a)# command-line-argumentsgo/src/nonexistent-website.com/example-2019-10-26/main.go:9: undefined: fmt in fmt.Println   2可以通过-ing 到保证是最新的目录来go build诱导发出绝对路径cd/go/src/nonexistent-website.com/example-2019-10-26$ (a=`realpath main.go` && cd `mktemp -d` && go build $a)# command-line-arguments/go/src/nonexistent-website.com/example-2019-10-26/main.go:9: undefined: fmt in fmt.Println   2
查看完整描述

1 回答

?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

除了包装绝对路径并粘贴到随机目录之外,是否有方法配置 go 工具来报告绝对路径?

不。


查看完整回答
反对 回复 2023-07-31
  • 1 回答
  • 0 关注
  • 86 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信