Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
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.$ 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