Home >Backend Development >Golang >Set workdir/pwd for go build
php editor Xiaoxin is here to introduce a method for setting the working directory of "go build". When developing using the Go language, we often need to execute the "go build" command in the terminal to compile our code. By default, this command sets the working directory to the current directory. But sometimes we want to set the working directory to another location to better organize our code and resource files. This article will show you how to set the working directory of "go build" on the command line for more flexible code compilation and debugging.
Is it possible to set the working directory to a different path?
For example, I want to run go build
from the root path, but my source code is in a different directory and I don't want to cd
to it.
For example, npm has --prefix
, which is used for this purpose.
Yes, it is possible.
go build -o [output file path/name] [source code file path/name]
For example, if your source code files are located in projectdir/code/src/
and you want to build the output and save it to projectdir/code/out
, do the following:
$ go build -o projectdir/code/out/main projectdir/code/src/main.go
According to go build
documentation:
If the specified output is an existing directory or begins with a slash or backslash, then any generated executable will be written to it Table of contents.
So our above build command can be rewritten as follows:
go build -o projectdir/code/out/ projectdir/code/src/main.go
It will generate an executable file named main
in the projectdir/code/out/
directory.
For more details, run go help build
The above is the detailed content of Set workdir/pwd for go build. For more information, please follow other related articles on the PHP Chinese website!