Dissecting "go install": Unraveling the Build and Installation Process
Often left unexplained in the documentation, the distinction between go build and go install can leave developers puzzled. While most expect go install to mirror the functionality of make install by relocating compiled executables to a designated location, they are surprised to find the results residing in the GOROOT/bin directory. This article aims to clarify the purpose and behavior of both commands.
What Actually Happens?
go build is a simple compilation tool. It merely assembles the executable file and stores it at the specified destination. go install, on the other hand, executes a slightly more complex process:
Visualizing the Package Tree
To illustrate the effects of go build and go install, consider the following package tree:
. ├── bin │ └── hello # by go install └── src └── hello ├── hello # by go build └── hello.go
The hello executable is compiled by go build and resides in the src/hello directory, while go install places it in $GOPATH/bin and stores the dependency caches in $GOPATH/pkg.
Can the Installation Location be Customized?
Unlike make install, go install does not provide an option to specify a custom installation directory. To achieve this level of control, it's necessary to create a Makefile that defines the build and installation process. However, this approach is generally not recommended.
Additional Context
For further insights, refer to the official documentation on the following topics:
The above is the detailed content of What's the Difference Between `go build` and `go install`, and Where Do They Install Executables?. For more information, please follow other related articles on the PHP Chinese website!