Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
These three commands do almost the same thing — but where the binary ends up and what gets cached are very different. go run compiles to a temp dir and executes once; go build produces a binary in your CWD; go install produces one in $GOPATH/bin for use as a CLI. Mixing them up wastes minutes per build. And once you understand them, the build cache (which is shared across all three) becomes the second-most-important Go performance feature after the compiler itself.
go run compiles the package to a temporary directory, runs it once, then discards the binary — a one-shot shortcut for scripts and exploratory use. go build compiles to your working directory (or a named output via -o), giving you a portable binary you can distribute or run repeatedly. go install goes further, placing the binary in $GOPATH/bin — which should be on your $PATH — making it a permanent CLI tool available anywhere on the machine. All three share the same build cache (go env GOCACHE), so after the first compile only changed packages are recompiled; a second go run on unchanged code takes milliseconds, not seconds.
main.go and find each output.time go run main.go vs time go build && time ./main. Note which is faster on a re-run.go clean -cache and run again. Note the difference — that's how much the cache buys you.GOOS=linux GOARCH=amd64 go build -o hi-linux main.go and check file hi-linux. We'll do this properly in task 8.Use these three in order. Each builds on the one before.
In one paragraph, explain when I'd use `go run`, `go build`, and `go install` — and why mixing up `go install` and `go build` is a common beginner mistake.
How does Go's build cache work under the hood? What does it key on, where does it live, and why is it safe to share across projects?
I'm shipping a CLI tool to teammates. Compare `go install`, GoReleaser, and Homebrew tap — what's the right delivery channel for a 5-person team vs a public OSS tool?
$ cat main.go
package main
import "fmt"
func main() { fmt.Println("hi") }
# 1) compile + run, no binary left behind
$ go run main.go
hi
# 2) compile to a binary in CWD
$ go build -o hi main.go
$ ls -lh hi
-rwxr-xr-x ... 1.9M hi
$ ./hi
hi
# 3) compile + install to $GOPATH/bin (for CLIs)
$ go install
$ ls $(go env GOPATH)/bin | grep hi
# the build cache makes repeat builds nearly free:
$ go env GOCACHE
/Users/you/Library/Caches/go-buildgo run main.go