Install Go
Install the Go toolchain, verify it, and run your first program with go run.
Installing Go
Download the current stable release (Go 1.22+) from the official Go downloads page for your OS, or use a package manager.
Windows — run the .msi installer, which sets up PATH automatically.
macOS (Homebrew):
brew install go
Linux (Debian/Ubuntu):
sudo rm -rf /usr/local/go
curl -LO https://go.dev/dl/go1.22.0.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
Verifying the install
go version
go version go1.22.0 linux/amd64
Your first program
mkdir hello && cd hello
go mod init hello # creates go.mod — declares this as a Go module
Create main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Run it:
go run main.go
Hello, World!
Build a standalone binary instead:
go build -o hello main.go
./hello
Go modules
go mod init hello created a go.mod file — Go's equivalent of package.json or pom.xml, declaring the module name and its dependency versions:
module hello
go 1.22
Adding a third-party dependency updates go.mod and go.sum (a lockfile of exact checksums) automatically:
go get github.com/gin-gonic/gin
Essential tooling
go fmt ./... # auto-formats every file — Go's formatting is non-negotiable and standardized
go vet ./... # static analysis — catches common mistakes before you even run tests
go test ./... # runs all tests in the module
Because gofmt produces one canonical formatting for all Go code, there's effectively no "tabs vs spaces" debate in the Go community — every codebase looks the same.
Common mistakes
- Forgetting
go mod init— without ago.mod,go getand dependency management won't work correctly in newer Go versions. - Ignoring
go vetwarnings, which often catch real bugs (e.g., aPrintfformat string that doesn't match its arguments). - Editing code without running
gofmt— most editors can run it automatically on save.
Interview questions
Q: What is go.mod and why does it matter?
It's the module manifest — it declares the module's import path, the Go version it targets, and its dependencies with exact versions, making builds reproducible across machines.
Q: What's the difference between go run and go build?
go run compiles to a temporary binary and executes it immediately, ideal for quick iteration. go build produces a persistent, named binary on disk meant for distribution or deployment.