New Go programmers often don't know or get confused what the fundamental go build command does.
What do exactly the go build
and go install
commands build and where do they put the result/output?
New Go programmers often don't know or get confused what the fundamental go build command does.
What do exactly the go build
and go install
commands build and where do they put the result/output?
What the
go
command does depends on whether we run it for a "normal" package or for the special"main"
package.For packages
go build
builds your package then discards the results.go install
builds then installs the package in your$GOPATH/pkg
directory.For commands (package
main
)go build
builds the command and leaves the result in the current working directory.go install
builds the command in a temporary directory then moves it to$GOPATH/bin
.Basically you can use
go build
as a check that the packages can be built (along with their dependencies) whilego install
also (permanently) installs the results in the proper folders of your$GOPATH
.go build
will silently terminate if everything is OK, and will give you error messages if the packages cannot be built/compiled.Whenever the
go
tool installs a package or binary, it also installs whatever dependencies it has, so runninggo install
will also install packages your program depends on (publicly available, "go gettable" packages), automatically.For a start, read the official How to Write Go Code page.
More information about the
go
tool: Command goYou can also get more help by running the following command:
It is also worth noting that starting with Go 1.5
go install
also removes executables created bygo build
(source):To complete the list,
go run
compiles your application into a temporary folder, and starts that executable binary. When the app exits, it properly cleans up the temporary files.Question inspired by Dave Cheney's What does go build build?
For package:
That won't be true after Go 1.10 (Q1 2018), thank to CL 68116 and CL 75473. See this thread, that I reference here.
Actually...
go install
will change also with Go 1.10, in addition of the new cache: