Installing and Running Go
Overview
Setting up a Go development environment is straightforward. This chapter covers installation, verification, and the fundamental go run and go build workflow that you’ll use throughout your Go journey.
Installation
macOS
Using Homebrew (recommended):
brew install goOr download from go.dev/dl and run the installer package.
Linux
Download and extract:
# Download the latest version (check go.dev/dl for current version)
wget https://go.dev/dl/go1.26.0.linux-amd64.tar.gz
# Remove any previous installation and extract
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.26.0.linux-amd64.tar.gz
# Add to PATH (add to ~/.bashrc or ~/.zshrc)
export PATH=$PATH:/usr/local/go/binWindows
Download the MSI installer from go.dev/dl and run it. The installer adds Go to your PATH automatically.
Verify Installation
$ go version
go version go1.24.0 darwin/arm64Environment Variables
| Variable | Purpose | Default |
|---|---|---|
GOROOT |
Go installation directory | Auto-detected |
GOPATH |
Workspace directory | $HOME/go |
GOBIN |
Binary installation directory | $GOPATH/bin |
Check your environment:
$ go env
# Shows all Go environment variables
$ go env GOPATH
/Users/yourname/goYour First Go Program
Create a file named hello.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}The go run Command
go run compiles and executes in one step—perfect for development:
$ go run hello.go
Hello, Go!How It Works
- Compiles the source to a temporary binary
- Executes that binary
- Cleans up the temporary file
Running Multiple Files
# Run multiple files
$ go run main.go utils.go
# Run all Go files in current directory
$ go run .The go build Command
go build creates a permanent executable:
$ go build hello.go
$ ls
hello hello.go
$ ./hello
Hello, Go!Common Build Options
# Specify output name
$ go build -o myapp hello.go
# Build for different OS/architecture
$ GOOS=linux GOARCH=amd64 go build -o myapp-linux
# Build with optimizations (strip debug info)
$ go build -ldflags="-s -w" -o myappCross-Compilation
Go makes cross-compilation trivial:
# Build for Linux from macOS
$ GOOS=linux GOARCH=amd64 go build -o app-linux
# Build for Windows
$ GOOS=windows GOARCH=amd64 go build -o app.exe
# Build for ARM (Raspberry Pi)
$ GOOS=linux GOARCH=arm GOARM=7 go build -o app-armThe Build Lifecycle
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Source Code │────▶│ Compiler │────▶│ Binary │
│ (.go) │ │ │ │ (executable)│
└─────────────┘ └─────────────┘ └─────────────┘
│
┌──────┴──────┐
│ │
┌─────▼────┐ ┌──────▼─────┐
│ go run │ │ go build │
│(temp bin)│ │(perm. bin) │
└──────────┘ └────────────┘
IDE and Editor Setup
Visual Studio Code
- Install the Go extension
- Open any
.gofile - Accept prompts to install Go tools
Recommended Tools (installed automatically)
gopls- Language serverdlv- Debuggerstaticcheck- Linter
GoLand
JetBrains GoLand is a commercial IDE with excellent Go support out of the box.
Neovim/Vim
Use gopls with your LSP client of choice (nvim-lspconfig, coc.nvim, etc.).
Project Structure Basics
myproject/
├── go.mod # Module definition
├── go.sum # Dependency checksums
├── main.go # Entry point
├── internal/ # Private packages
│ └── config/
│ └── config.go
└── pkg/ # Public packages (optional)
└── utils/
└── utils.go
Common Pitfalls
GOPATH vs Modules
Modern Go uses modules (introduced in Go 1.11). You don’t need to work inside $GOPATH/src anymore:
# Initialize a new module anywhere
$ mkdir myproject && cd myproject
$ go mod init github.com/username/myprojectExecutable vs Library
Only package main with a main() function creates executables:
// This creates a binary
package main
func main() { }// This is a library (cannot run directly)
package mylib
func Helper() { }Summary
| Command | Purpose |
|---|---|
go run |
Compile and run (temporary) |
go build |
Compile to binary |
go install |
Compile and install to $GOBIN |
go env |
Show environment variables |
go version |
Show Go version |
Exercises
- Install Go and verify with
go version - Create a “Hello, World!” program and run it with
go run - Build the same program with
go buildand execute the binary - Cross-compile for a different operating system