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.
CodePudding user response:
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
CodePudding user response:
No, this is not possible. Just cd into it.