1. Problem background
Install Go librarydb2struct
When usinggo get
Command execution
go get ``/Shelnutt2/db2struct/cmd/db2struct
,
implementdb2struct -h
Report an error:
zsh: command not found: db2struct
Change togo install
Command execution
go install ``/Shelnutt2/db2struct/cmd/db2struct
After successful executiondb2struct -h
,at the same timels $GOPATH/bin
I also saw the executable filedb2struct
。
2. Exploration of causes
(I) Command definition and difference
go install
andgo get
They 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/src
The corresponding location in the directory and compile the programs and libraries in the code package. If the target package has been downloaded before,go get
It will try to update to the latest version and recompile the program and library files. After updating the code package,go get
It will also automatically copy the executable file of the downloaded code package to$GOPATH/bin
In 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/bin
or$GOPATH/pkg
In 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/bin
In the directory (if the project is a library, the generated.a
and store the file to$GOPATH/pkg
under the directory).
therefore,go get
Used to download and update code packages and generate corresponding executable programs, andgo install
Used 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 get
andgo install
Make 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 get
Is it includedgo install
Can be consideredgo get
The command containsgo install
command functions, but there are still some nuances between them.
go get
Commands 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/bin
In the directory, to simplify the operation of the program.go get
The 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 install
The 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/bin
or$GOPATH/pkg
In the directory. at the same time,go install
The command can also be specified by-ldflags
Parameters 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 get
The command has already used the Go module management tool by default. andgo install
The 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 get
The command containsgo install
Some functions of the command, but in some special cases, it still needs to be usedgo install
Commands for more flexible compilation and installation operations.
(III) Why use itgo install
In usego get
When 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 install
Commands can avoid these problems. The following lists several common situations:
-
Version incompatible:if
go get
The code package downloaded by the command is incompatible with the currently used Go language version, sogo get
The command may generate a compilation error or generate an executable file that cannot be run. For this case, usego install
The command can ensure that it is a compiler compatible with the Go language version on the current system. -
Different compilation environments: In use
go get
When 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 install
When 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 use
go get
When 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 install
The 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 get
The command is very convenient and fast, but it still needs to be used when encountering some special situations.go install
Commands for more stable compilation and installation effects.
You can use the following table to comparego get
andgo install
The 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!