设置go build的工作目录/当前目录。

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

Set workdir/pwd of go build

问题

可以将工作目录设置为不同的路径吗?

例如,我想从根路径运行 go build,但我的源代码位于不同的目录下,我不想使用 cd 命令切换到该目录。

例如,npm 提供了 --prefix 参数,用于此目的。

英文:

Is it possible to set the workdir to a different path?

For example, I want to run go build from the root path, but my source code is under a different directory, and I do not want to cd to it.

npm, for example, has the --prefix, which serves for this purpose.

答案1

得分: 3

是的,这是可能的。

go build -o [输出文件路径/名称] [源代码文件路径/名称]

例如,如果你的源代码文件位于 projectdir/code/src/,并且想要构建并保存输出到 projectdir/code/out,请执行以下操作:

$ go build -o projectdir/code/out/main projectdir/code/src/main.go

根据 go build 的文档:

如果指定的输出是一个已存在的目录,或者以斜杠或反斜杠结尾,那么生成的可执行文件将被写入该目录。

因此,我们上面的构建命令可以这样重写:

go build -o projectdir/code/out/ projectdir/code/src/main.go

它将在 projectdir/code/out/ 目录中生成名为 main 的可执行文件。

要获取更多详细信息,请运行 go help build

英文:

Yes, its possible.

go build -o [output file path/name] [source code file path/name]

For example, if your source code file is located in projectdir/code/src/ and want to build and save output to projectdir/code/out, do following:

$ go build -o projectdir/code/out/main projectdir/code/src/main.go

As per go build documentation:

> If the named output is an existing directory or ends with a slash or
> backslash, then any resulting executables will be written to that
> directory.

So our above build command can be rewritten like this:

go build -o projectdir/code/out/ projectdir/code/src/main.go

and it will generate executable named main in projectdir/code/out/ directory.

For more details, run go help build

答案2

得分: -3

不,这在1.19版本中是不可能的。只需进入该目录。

Go 1.20(尚未发布)引入了-C标志,可能是你想要的。

英文:

No, this is not possible with 1.19. Just cd into it.

Go 1.20 (yet unreleased) brings the -C flag which might be what you want.

huangapple
  • 本文由 发表于 2023年1月15日 20:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75124987.html
匿名

发表评论

匿名网友

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

确定