Home > Backend Development > Golang > What's the Difference Between `go build` and `go install`, and Where Do They Install Executables?

What's the Difference Between `go build` and `go install`, and Where Do They Install Executables?

DDD
Release: 2024-12-03 18:41:11
Original
364 people have browsed it

What's the Difference Between `go build` and `go install`, and Where Do They Install Executables?

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:

  • It compiles the executable file, just like go build.
  • It moves the executable to $GOPATH/bin, ensuring it's accessible from your command-line.
  • It caches all non-main packages imported by the program in $GOPATH/pkg. This cache is leveraged during subsequent compilations, provided the source code hasn't changed.

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

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:

  • [go build](https://go.dev/ref/cmd/go#hdr-Compile_packages_and_dependencies)
  • [go install](https://go.dev/ref/cmd/go#hdr-Install_and_build_packages_and_dependencies)

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!

source:php.cn
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