Home > Backend Development > Golang > Set workdir/pwd for go build

Set workdir/pwd for go build

WBOY
Release: 2024-02-15 14:00:11
forward
971 people have browsed it

设置 go build 的 workdir/pwd

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.

Question content

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.

Solution

Yes, it is possible.

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

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
Copy after login

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
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template