Introduction
Go ships with a rich set of command-line tools that come standard with every installation, so teams rarely need to argue about code style or reach for external build systems. gofmt enforces one canonical formatting style, go vet flags suspicious constructs that compile but are likely bugs, and go build/go run handle compiling and executing programs. Together these tools reduce friction in day-to-day development and code review.
Cricket analogy: A board that mandates one official pitch-curating standard across all stadiums, rather than letting each curator improvise, mirrors gofmt's one canonical style; a match referee flagging suspicious no-ball actions before the umpire calls it mirrors go vet, and a standard practice-net setup mirrors go build/go run.
Syntax
# Format all files in the current directory (writes changes in place)
gofmt -w .
# Equivalent, more commonly used form
go fmt ./...
# Run static analysis checks
go vet ./...
# Compile a package into a binary
go build ./...
# Compile and run without leaving a binary behind
go run main.goExplanation
gofmt (usually invoked as go fmt) rewrites source files into Go's single canonical style — consistent indentation, spacing, and alignment — so that all Go code looks the same regardless of who wrote it, eliminating formatting debates in code review. go vet performs static analysis to catch mistakes that compile successfully but are almost certainly wrong, such as a Printf call whose format verbs don't match its arguments, or a struct copied while holding a sync.Mutex. go build compiles packages and their dependencies into an executable (or verifies compilation without producing one, depending on flags), while go run combines compiling and executing in one step, discarding the binary afterward — handy for quick iteration. go install compiles a package and installs the resulting binary into the Go bin directory for later reuse. Beyond the standard toolchain, the community widely uses additional linters such as golint (now largely superseded) and staticcheck, which perform deeper style and correctness checks than go vet alone.
Cricket analogy: gofmt is like a standardized scorecard layout every scorer uses so any team can read it instantly; go vet is like a third umpire spotting a mismatched replay angle or a fielder copying the captain's armband mid-over, a mutex copied by value; go build is preparing the full XI, go run is a quick friendly played and forgotten, go install is adding a proven player to the permanent squad, and staticcheck is an extra performance analyst catching subtler issues.
Example
package main
import "fmt"
func main() {
name := "Gopher"
age := 15
// Bug: %d expects an int, but two args are passed for one verb.
fmt.Printf("Hello %s, age %d\n", name, age, age)
}Output
$ go vet ./...
./main.go:8:5: fmt.Printf call needs 2 args but has 3
$ gofmt -l .
main.go
$ go build ./...
# builds successfully even though go vet flagged an issue,
# because vet warnings do not block compilationKey Takeaways
- gofmt (go fmt) enforces Go's single canonical formatting style automatically, removing style debates.
- go vet performs static analysis to catch likely bugs that still compile, such as mismatched Printf verbs.
- go build compiles packages/dependencies into a binary; go run compiles and executes without keeping the binary.
- go install builds a binary and places it in the Go bin directory for reuse across the system.
- Third-party linters like staticcheck (and the older golint) offer deeper checks beyond go vet.
Practice what you learned
1. What is the primary purpose of gofmt?
2. Which tool would flag a Printf call whose format verbs don't match its arguments?
3. What is the key difference between go build and go run?
4. What does go install do?
5. Which of these is a popular third-party static analysis tool beyond go vet?
Was this page helpful?
You May Also Like
Testing in Go
Write and run automated tests in Go using the built-in testing package, table-driven tests, and benchmarks.
Packages and Go Modules
Learn how Go organizes code into packages and manages dependencies with go.mod using the modules system.
Setting Up a Go Environment
Step-by-step guide to installing Go, understanding modules, and running your first program.
Error Handling in Go
Learn Go's idiomatic error handling using the built-in error interface and explicit error return values instead of exceptions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics