构建移除信息

huangapple go评论76阅读模式
英文:

Go Build remove information

问题

我已经可以使用Go Build命令来去除项目的当前目录信息,或者去除GOPATH信息,像下面这样,现在我只能分别删除其中一个:

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

但是我不知道如何同时删除它们,我不知道如何组合它们。

在Windows上,我尝试了这个:

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

但是它没有起作用。

最终结果

@ZekeLu,答案是正确的。

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

英文:

I can already use Go Build to remove the current directory information of the project, or I can remove Gopath information, like the following, I can only delete one of them separately now

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

But I don’t know how to remove them at the same time, I don’t know how to combine it

on windows, i try this

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

It does not work

Final Results

@ZekeLu,the answer is right.

I want to explain what happened, I expected to remove GOROOT information, but I wrote GOPATH, so I thought @ZekeLu's second answer was wrong, now I have modified and tested, there is no problem, two answers all right

答案1

得分: 0

尝试使用以下命令:

go build -trimpath

这是关于-trimpath标志的文档,适用于Go命令

-trimpath

从生成的可执行文件中删除所有文件系统路径。
记录的文件名将以模块路径@版本(使用模块时)或纯导入路径(使用标准库或GOPATH时)开头,而不是绝对文件系统路径。

如果你只想删除一些前缀,可以像这样操作:

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

编译命令汇编命令都调用了objabi.ApplyRewrites来修剪路径。根据objabi.ApplyRewrites的实现rewrites参数是一个以分号分隔的重写列表。

// ApplyRewrites根据重写参数对给定目录中的文件进行重写,并返回文件名。
//
// 重写参数是一个以分号分隔的重写列表。
// 每个重写的格式为“prefix”或“prefix=>replace”,
// 其中prefix必须匹配路径元素的前导序列,
// 并且可以完全删除或替换为替换字符串。
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
}
英文:

Try this command:

go build -trimpath

Here is the doc for the -trimpath flag for the Go command:

> -trimpath
>
> remove all file system paths from the resulting executable.
Instead of absolute file system paths, the recorded file names
will begin either a module path@version (when using modules),
or a plain import path (when using the standard library, or GOPATH).

If you only want to remove some of the prefixes, you can do it like this:

go build -gcflags &quot;all=-trimpath=%CD%;%GOPATH%&quot; -asmflags &quot;all=-trimpath=%CD%;%GOPATH%&quot;

Both the Compile command and the Asm command calls objabi.ApplyRewrites to trim paths. According to the implementation of objabi.ApplyRewrites, The rewrites argument is a ;-separated list of 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 &quot;prefix&quot; or &quot;prefix=&gt;replace&quot;,
// 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 &lt;= len(rewrites); i++ {
		if i == len(rewrites) || rewrites[i] == &#39;;&#39; {
			if new, ok := applyRewrite(file, rewrites[start:i]); ok {
				return new, true
			}
			start = i + 1
		}
	}

	return file, false
}

huangapple
  • 本文由 发表于 2023年7月3日 10:37:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定