Home >Backend Development >Golang >What's the Difference Between `go build` and `go install` in Go?

What's the Difference Between `go build` and `go install` in Go?

DDD
DDDOriginal
2024-12-02 18:01:15340browse

What's the Difference Between `go build` and `go install` in Go?

What Actually Does Go Install Do?

The Go documentation doesn't explain the difference between go build and go install in detail. One might expect install to follow the make install pattern—that it takes the compiled artifacts and places them in their final designated location. However, in the case of go install, it places them in GOROOT/bin instead.

Go Build vs Go Install

go build solely compiles the executable file and moves it to the specified destination. On the other hand, go install performs additional tasks:

  • Moves the executable file to $GOPATH/bin
  • Caches all non-main packages that are imported into $GOPATH/pkg

The cached dependencies are then utilized in subsequent compilations, provided the source code remains unchanged.

A Visualization of the Package Tree

To illustrate the outcomes of using go build and go install:

├── bin
│   └── hello  # by go install
└── src 
    └── hello
        ├── hello  # by go build
        └── hello.go

Note: go build generates the executable file within the current directory, while go install places it in $GOPATH/bin.

For more detailed information and advanced functionalities, refer to the official Go documentation: [https://go.dev/doc/install/troubleshooting#how-go-install-works](https://go.dev/doc/install/troubleshooting#how-go-install-works)

The above is the detailed content of What's the Difference Between `go build` and `go install` in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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