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):

Bash
brew install go

Linux (Debian/Ubuntu):

Bash
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

Bash
go version
Plaintext
go version go1.22.0 linux/amd64

Your first program

Bash
mkdir hello && cd hello
go mod init hello    # creates go.mod — declares this as a Go module

Create main.go:

Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Run it:

Bash
go run main.go
Plaintext
Hello, World!

Build a standalone binary instead:

Bash
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:

Plaintext
module hello

go 1.22

Adding a third-party dependency updates go.mod and go.sum (a lockfile of exact checksums) automatically:

Bash
go get github.com/gin-gonic/gin

Essential tooling

Bash
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 a go.mod, go get and dependency management won't work correctly in newer Go versions.
  • Ignoring go vet warnings, which often catch real bugs (e.g., a Printf format 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.