英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论