By icza

2015-06-03 06:39:22 8 Comments

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?


@icza 2015-06-03 06:39:22

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) while go 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 running go 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 go

You can also get more help by running the following command:

go help build

It is also worth noting that starting with Go 1.5 go install also removes executables created by go build (source):

If 'go install' (with no arguments, meaning the current directory) succeeds, remove the executable written by 'go build', if present. This avoids leaving a stale binary behind...

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?

@Scott Stensland 2017-04-13 17:37:28

seems strange that go install does not update the executable if its identical to previously installed one ... any insights here ?

@VonC 2017-11-04 10:46:09

For package:

go build: builds your package then discards the results

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.

What do exactly the go build and go install commands build

Whenever the go tool installs a package or binary, it also installs whatever dependencies it has, so running go install will also install packages your program depends on (publicly available, "go gettable" packages), automatically.

Actually... go install will change also with Go 1.10, in addition of the new cache:

The "go install" command no longer installs dependencies of the named packages (CL 75850).

If you run "go install foo", the only thing installed is foo.

Before, it varied. If dependencies were out-of-date, "go install" also installed any dependencies.
The implicit installation of dependencies during "go install" caused a lot of confusion and headaches for users, but it was previously necessary to enable incremental builds.
Not anymore.
We think that the new "install what I said" semantics will be much more understandable, especially since it's clear from bug reports that many users already expected them.
To force installation of dependencies during "go install", use the new "go install -i", by analogy with "go build -i" and "go test -i".

The fact that "go install" used to install any rebuilt dependencies caused confusion most often in conjunction with -a, which means "force rebuild of all dependencies".
Now, "go install -a myprog" will force a complete rebuild of all dependencies of myprog, as well as myprog itself, but only myprog will get installed. (All the rebuilt dependencies will still be saved in the build cache, of course.)
Making this case work more understandably is especially important in conjunction with the new content-based staleness analysis, because it sees good reasons to rebuild dependencies more often than before, which would have increased the amount of "why did my dependencies get installed" confusion.
For example, if you run "go install -gcflags=-N myprog", that installs a myprog built with no compiler optimizations, but it no longer also reinstalls the packages myprog uses from the standard library without compiler optimizations.

@ctrl-alt-delor 2019-01-20 14:09:56

Does go build, do gets? I have a build error cannot find package "" in any of:…. I don't know how to tell it to get it. Do I need to explicitly get?

@VonC 2019-01-20 14:12:07

@ctrl-alt-delor With which version of Go? Does your project has a go.mod file in it?

@ctrl-alt-delor 2019-01-20 14:16:06

go version go1.11.4 linux/amd64. I don't know about go.mod. I am re-building‌​e/main.go, it is odd as I just built the whole package, and am using this example as a base, and I did create a more basic version that worked (but not using this library). I can't see how it was not installed with the mustache package.

@VonC 2019-01-20 14:18:37

@ctrl-alt-delor so cobr is vendored‌​/…. Is your GOPATH properly set?

@ctrl-alt-delor 2019-01-20 14:41:45

I have added a formal question here

@ctrl-alt-delor 2019-01-20 15:28:19

I have discovered what you already found. The package is in a vendor sub-directory: this is why it did not get installed. However I don't know why it does not install it now on build. Or how to use the vendor directory (if I copy it to my directory).

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] Xcode "Build and Archive" from command line

5 Answered Questions

20 Answered Questions

[SOLVED] Xcode warning: "Multiple build commands for output file"

27 Answered Questions

[SOLVED] iOS - Build fails with CocoaPods cannot find header files

18 Answered Questions

[SOLVED] What does a just-in-time (JIT) compiler do?

8 Answered Questions

[SOLVED] What is a build tool?

  • 2011-08-30 21:12:21
  • Gaurang Agrawal
  • 51808 View
  • 97 Score
  • 8 Answer
  • Tags:   build

8 Answered Questions

[SOLVED] What does the Visual Studio "Any CPU" target mean?

9 Answered Questions

1 Answered Questions

[SOLVED] How can I see the internal compile commands which fail in a "go get" installation?

  • 2015-07-10 15:56:04
  • timbl
  • 5213 View
  • 125 Score
  • 1 Answer
  • Tags:   go

Sponsored Content