SoFunction
Updated on 2025-03-01

Comparative introduction of go get and go install

1. Problem background

Install Go librarydb2structWhen usinggo getCommand execution

go get ``/Shelnutt2/db2struct/cmd/db2struct

implementdb2struct -hReport an error:

zsh: command not found: db2struct

Change togo installCommand execution

go install ``/Shelnutt2/db2struct/cmd/db2struct

After successful executiondb2struct -h,at the same timels $GOPATH/binI also saw the executable filedb2struct

2. Exploration of causes

(I) Command definition and difference

go installandgo getThey are all tool commands for Go, but there are some differences between them.

  • go get: Used to download or update Go code packages from remote code repositories such as GitHub. It downloads the code package and stores it in$GOPATH/srcThe corresponding location in the directory and compile the programs and libraries in the code package. If the target package has been downloaded before,go getIt will try to update to the latest version and recompile the program and library files. After updating the code package,go getIt will also automatically copy the executable file of the downloaded code package to$GOPATH/binIn the directory, to facilitate direct use of the executable file.
  • go install: Used to compile and install Go code packages and store the executable program or library files it generates to$GOPATH/binor$GOPATH/pkgIn the directory. If you execute it in the project directorygo install, it will compile and install the code of the current project, generate the executable file and save it to$GOPATH/binIn the directory (if the project is a library, the generated.aand store the file to$GOPATH/pkgunder the directory).

therefore,go getUsed to download and update code packages and generate corresponding executable programs, andgo installUsed to convert a Go code package into an executable program or library file and install it to the system path for direct use.

Use table form pairgo getandgo installMake a comparison:

Order Function Download location Generate file location
go get Download or update Go code packages from the remote code base $GOPATH/src $GOPATH/bin
go install Compile and install Go code packages No need to download your own code, $GOPATH/src is used for searching of dependencies GOPATH/bin or GOPATH/bin or GOPATH/bin or GOPATH/pkg

(two)go getIs it includedgo install

Can be consideredgo getThe command containsgo installcommand functions, but there are still some nuances between them.

go getCommands are used to download or update Go code packages from remote code bases and automatically compile and generate corresponding executable programs, while also installing them to$GOPATH/binIn the directory, to simplify the operation of the program.go getThe command does not need to specify the path to the package. It uses the package import path agreed in the Go language specification to obtain package information and automatically process the dependencies of the package for easy compilation.

By comparison,go installThe command is more flexible, it can compile and install the code of the current project, generate executable files or libraries, and save them in$GOPATH/binor$GOPATH/pkgIn the directory. at the same time,go installThe command can also be specified by-ldflagsParameters to modify the link flag of the binary file, thereby adding some custom meta information to the binary file, so that this information can be read through code at runtime.

After Go version 1.16,go getThe command has already used the Go module management tool by default. andgo installThe command always needs to enter the path under the Go module management path before using this command to compile and install the relevant code package.

To sum up, it can be consideredgo getThe command containsgo installSome functions of the command, but in some special cases, it still needs to be usedgo installCommands for more flexible compilation and installation operations.

(III) Why use itgo install

In usego getWhen the command downloads and installs the code package, the generated executable file may not take effect in some cases, which is usually caused by incompatible Go versions or different compilation environments. and usego installCommands can avoid these problems. The following lists several common situations:

  • Version incompatible:ifgo getThe code package downloaded by the command is incompatible with the currently used Go language version, sogo getThe command may generate a compilation error or generate an executable file that cannot be run. For this case, usego installThe command can ensure that it is a compiler compatible with the Go language version on the current system.
  • Different compilation environments: In usego getWhen commands download code packages, sometimes their compilation environment is different from the current environment, which may cause the executable to not work properly. In usego installWhen commanded, the command will recompile the source code in the current environment and generate an executable file, so that the code can be run more stably.
  • Complex dependency relationship: In usego getWhen commands download large code packages, it may automatically download and install a large number of dependencies. This can make dependencies complicated and difficult to manage, and sometimes errors or warnings may occur when the dependencies are downloaded or compiled. andgo installThe command will directly look for dependencies from the corresponding dependencies in the current path, so it can avoid the complexity and errors caused by this situation.

To sum up, althoughgo getThe command is very convenient and fast, but it still needs to be used when encountering some special situations.go installCommands for more stable compilation and installation effects.

You can use the following table to comparego getandgo installThe difference in compilation:

Order Function Compilation effect Application scenarios
go get Download or update Go code packages and generate executable files The generated executable file may not run or other problems may occur due to different versions or compile environments. You need to quickly download and install the specified version of Go code package. If the generated executable file cannot run, you need to recompile it using the go install command.
go install Compile and install Go code and generate executable files or libraries Recompile the source code and generate executable files or libraries in the current environment, allowing you to run code packages more stably Go code needs to be recompiled and binary files are generated in the current environment. Compilation errors and generated executable files cannot run due to version incompatibility, different compilation environments or complexity of dependencies.

This is the article about the comparison and introduction of go get and go install. For more related contents of go get and go install, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!