我已经可以使用Go Build删除项目的当前目录信息,也可以删除Gopath信息,如下所示,我现在只能单独删除其中的一个

go build -gcflags "all=-trimpath=${GOPATH}" -asmflags "all=-trimpath=${GOPATH}"

但我不知道如何同时go 除它们,我不知道如何组合它们

在Windows上,我try 了一下

go build -gcflags "all=-trimpath=%CD%" -asmflags "all=-trimpath=%CD%" -gcflags "all=-trimpath=%GOPATH%" -asmflags "all=-trimpath=%GOPATH%"

它不起作用了

最终结果

@泽克鲁,答案是对的.

我想解释一下发生了什么,我本想删除GOROOT信息,但我写了GOPATH,所以我认为@ZekeLu的第二个答案是错误的,现在我已经修改和测试了,没有问题,两个答案都对

推荐答案

try 使用此命令:

go build -修剪路径

这是the Go command-修剪路径旗帜的文件:

-修剪路径

从生成的可执行文件中删除所有文件系统路径. 记录的文件名不是绝对文件系统路径 将开始一个模块路径@版本(当使用模块时), 或普通导入路径(当使用标准库或GOPATH时).

如果您只想删除一些前缀,可以这样做:

go build -gcflags "all=-修剪路径=%CD%;%GOPATH%" -asmflags "all=-修剪路径=%CD%;%GOPATH%"

the Compile commandthe Asm command都调用objabi.ApplyRewrites来修剪路径.根据the implementation of objabi.ApplyRewrites的说法,rewrites参数是以;分隔的重写列表.

// ApplyRewrites returns the filename for file in the given directory,
// as rewritten by the rewrites argument.
//
// The rewrites argument is a ;-separated list of rewrites.
// Each rewrite is of the form "prefix" or "prefix=>replace",
// where prefix must match a leading sequence of path elements
// and is either removed entirely or replaced by the replacement.
func ApplyRewrites(file, rewrites string) (string, bool) {
    start := 0
    for i := 0; i <= len(rewrites); i++ {
        if i == len(rewrites) || rewrites[i] == ';' {
            if new, ok := applyRewrite(file, rewrites[start:i]); ok {
                return new, true
            }
            start = i + 1
        }
    }

    return file, false
}

Go相关问答推荐

在不耗尽资源的情况下处理S3文件下载

如何用Golang解码这个嵌套的json?

使用!NOT运算符的Golang文本/模板多个条件

将这两个函数合二为一的惯用方法

按位移计算结果中的差异

使用golang sqlc中的引用参数

在 Cloud Run 中找不到默认凭据

你如何在 Golang 代码中测试 filepath.Abs​​ 失败?

我如何使用 TOML fixtures 在使用 Go Buffalo 框架的开发环境中为我的数据库 seeder ?

如何确定作为函数参数传递的指针是否正在被修改或副本是否正在被修改?

对所有标志进行 ORing 的简短方法

为什么import和ImportSpec之间可以出现多行注释,而PackageName和ImportPath之间不能出现?

Go Fyne 禁用 HSplit 调整大小?

Go 使用 struct 作为接口而不实现所有方法

有没有办法在一个 goroutine 返回后延迟后取消上下文?

Go:如何创建一个可以提供配置文件中描述的 url 的服务器

Go 赋值涉及到自定义类型的指针

在 VSCode 中使用命令行参数调试 Go 测试

有没有办法在golang中映射一组对象?

Go generics:我会在哪里使用 any 而不是 interface{}?