go build:go build is used to test the compiled package, mainly checking whether there are compilation errors. If it is an executable file The source code (that is, the main package) will directly generate an executable file.
go install: go install has two steps: the first step is to compile the imported package files. The main program will not be compiled until all imported package files are compiled; the second step is Place the compiled executable file in the bin directory ($GOPATH/bin), and the compiled package file in the pkg directory ($GOPATH/pkg).
go build
By go build plus the name of the Go source file to be compiled, we can get an executable file, by default The name of this file is the source file name minus the .go suffix.
$ go build hello.go $ lshello hello.go
Of course we can also specify other names through the -o option:
$ go build -o mygo hello.go $ lsmygo hello.go
If we directly execute the go build command in the go-examples directory without a file name, we will get An executable file with the same name as the directory name:
$ go build $ lsgo-examples hello.go
go install
Compared with the build command, the install command also The executable file or library file will be installed in the agreed directory.
The executable file compiled by go install is named after the directory name (DIR) where it is located.
go install installs the executable file into the bin directory at the same level as src. The bin directory is created by go install automatically creates
go install compiles the various packages that the executable file depends on and places them in the pkg directory at the same level as src
Recommended: go language tutorial
The above is the detailed content of The difference between go build and go install in go language. For more information, please follow other related articles on the PHP Chinese website!