Setting Up Your Development Environment
Setting up a Go development environment is remarkably straightforward. Go ships as a single binary and you can start coding immediately after installation. This section covers everything from OS-specific installation to editor configuration and the essential commands you'll use every day.
Installing Goβ
Linuxβ
Method 1: Official tar archive (recommended)
# Remove any existing installation
sudo rm -rf /usr/local/go
# Download Go 1.24 (latest stable as of 2024)
wget https://go.dev/dl/go1.24.0.linux-amd64.tar.gz
# Extract to /usr/local
sudo tar -C /usr/local -xzf go1.24.0.linux-amd64.tar.gz
# Add to PATH (add to ~/.bashrc or ~/.zshrc)
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc
# Verify installation
go version
# Output: go version go1.24.0 linux/amd64
Method 2: Package manager (Ubuntu/Debian)
# Via snap (keeps Go up to date automatically)
sudo snap install go --classic
# apt-get (Ubuntu repository version may be outdated β not recommended)
sudo apt-get install golang-go
macOSβ
Method 1: Homebrew (recommended)
# Install Homebrew if you don't have it
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Go
brew install go
# Verify installation
go version
# Output: go version go1.24.0 darwin/arm64
Method 2: Official installer
- Go to https://go.dev/dl/
- Download
go1.24.0.darwin-amd64.pkg(Intel) orgo1.24.0.darwin-arm64.pkg(Apple Silicon) - Run the PKG file and follow the prompts
- Open a new terminal and run
go versionto confirm
Windowsβ
Method 1: MSI installer (recommended)
- Go to https://go.dev/dl/
- Download
go1.24.0.windows-amd64.msi - Run the MSI file and follow the installer
- Open a new PowerShell or CMD window and verify:
go version
# Output: go version go1.24.0 windows/amd64
Method 2: Scoop (package manager)
# Install Scoop if you don't have it
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex
# Install Go
scoop install go
Environment Variablesβ
After installing Go, there are a few key environment variables to understand.
# Print all Go environment variables
go env
Key environment variables:
| Variable | Description | Default |
|---|---|---|
GOROOT | Go standard library installation path | /usr/local/go |
GOPATH | Go workspace path (module cache, etc.) | ~/go |
GOMODCACHE | Downloaded module cache location | $GOPATH/pkg/mod |
GOBIN | Binary install path for go install | $GOPATH/bin |
GOOS | Target OS for builds | Current OS |
GOARCH | Target architecture for builds | Current architecture |
GOPROXY | Module download proxy | https://proxy.golang.org |
Add GOPATH/bin to your PATH(required to run tools installed with go install):
# Add to ~/.bashrc or ~/.zshrc
export PATH=$PATH:$(go env GOPATH)/bin
Check specific environment variables:
go env GOPATH # /home/user/go
go env GOROOT # /usr/local/go
go env GOPROXY # https://proxy.golang.org,direct
go env GOMODCACHE # /home/user/go/pkg/mod
GOPATH vs Go Modulesβ
Go has gone through two approaches to dependency management. Go Modules have been the default since Go 1.16, and the GOPATH approach is no longer recommended.
GOPATH Approach (Legacy β not recommended)β
~/go/
src/
github.com/
username/
myproject/ β project code
pkg/ β compiled packages
bin/ β binaries
- All projects must live under
$GOPATH/src - Version management is difficult (cannot use different versions of the same package)
go getalways fetches the latest version
Go Modules Approach (Current Standard)β
~/projects/
myproject/ β create projects anywhere
go.mod β module definition file
go.sum β checksum lock file
main.go
- Projects can be created in any directory
- Reproducible builds guaranteed through precise version locking
- Dependencies are explicitly declared in
go.mod
Creating Your First Projectβ
# Create a project directory
mkdir hello-go && cd hello-go
# Initialize a module (module name is conventionally a GitHub path)
go mod init github.com/username/hello-go
# A go.mod file is created
cat go.mod
Contents of go.mod:
module github.com/username/hello-go
go 1.24
module: The unique name of this module (used as the import path)go: The Go version being used
Example go.mod after adding dependencies:
module github.com/username/hello-go
go 1.24
require (
github.com/fatih/color v1.16.0
github.com/gin-gonic/gin v1.9.1
)
The go.sum file stores cryptographic hashes of each dependency to prevent tampering. Never edit it manually β it's managed automatically by go mod tidy.
VS Code Setupβ
VS Code is the most widely used editor for Go development.
Installing the Go Extensionβ
- Open VS Code
- Open the Extensions Marketplace (
Ctrl+Shift+X) - Search for "Go" and install Go (by Go Team at Google)
- After restarting VS Code, open any
.gofile and it will prompt you to installgopls
Or from the command line:
code --install-extension golang.go
Go Language Server (gopls)β
gopls is the official Go language server, providing the foundation for all IDE features: autocomplete, go-to-definition, refactoring, and error highlighting.
# Manual installation
go install golang.org/x/tools/gopls@latest
Recommended settings.jsonβ
{
"go.useLanguageServer": true,
"go.formatTool": "gofmt",
"go.lintTool": "golangci-lint",
"go.lintOnSave": "package",
"go.formatOnSave": true,
"go.testOnSave": false,
"go.coverOnSave": false,
"go.buildOnSave": "package",
"go.vetOnSave": "package",
"editor.formatOnSave": true,
"[go]": {
"editor.defaultFormatter": "golang.go",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
}
}
Debugging Configuration (launch.json)β
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": [],
"env": {}
}
]
}
GoLand Basic Setupβ
JetBrains GoLand is a commercial Go-specific IDE offering powerful refactoring and debugging capabilities.
- Download from https://www.jetbrains.com/go/
- After installing, set the Go SDK path under Settings β Go β GOROOT
- Verify that module mode is enabled under Settings β Go β Go Modules
- Free licenses available for students and open source projects
Complete go Command Referenceβ
| Command | Description | Example |
|---|---|---|
go run | Compile and run source files immediately | go run main.go |
go build | Compile the current package to a binary | go build -o app ./... |
go test | Run test files (_test.go) | go test ./... |
go mod init | Initialize a new module | go mod init github.com/user/proj |
go mod tidy | Remove unused dependencies, add missing ones | go mod tidy |
go get | Add or update a package at a specific version | go get github.com/gin-gonic/gin@v1.9.1 |
go install | Install a package as a binary to GOBIN | go install github.com/air-verse/air@latest |
go fmt | Auto-format code (wrapper around gofmt) | go fmt ./... |
go vet | Static analysis for potential bugs | go vet ./... |
go doc | Print package/function documentation | go doc fmt.Println |
go env | Print Go environment variables | go env GOPATH |
go clean | Remove build cache and generated files | go clean -cache |
go list | List packages | go list ./... |
go work | Manage workspaces (multi-module) | go work init |
Common Command Examplesβ
# Run all tests in the project with verbose output
go test -v ./...
# Run only a specific test function
go test -run TestMyFunction ./...
# Measure test coverage
go test -cover ./...
# Build and run immediately
go build -o app ./... && ./app
# Update all dependencies
go get -u ./...
# Clean up unused dependencies
go mod tidy
# Open package documentation in a browser
go doc -http=:6060
gofmt β Automatic Formattingβ
Go ships with gofmt, a tool that enforces a single code style at the language level. There's no tab vs. spaces debate β Go uses tabs.
# Show diff for a single file (no modification)
gofmt -d main.go
# Format a single file in place
gofmt -w main.go
# Format the entire project
go fmt ./...
Before formatting:
package main
import "fmt"
func main(){
fmt.Println("hello")
x:=1+2
}
After formatting:
package main
import "fmt"
func main() {
fmt.Println("hello")
x := 1 + 2
_ = x
}
Configuring your editor to run gofmt automatically on save is the standard practice in the Go community.
Summaryβ
Here's what we covered in this section:
- OS-specific Go 1.24 installationβ Linux (tar), macOS (Homebrew), Windows (MSI)
- Environment variablesβ the roles of
GOROOT,GOPATH,GOBIN, andGOPROXY - Go Modulesβ the standard dependency management approach for modern Go projects
- VS Code setupβ
gopls, auto-formatting, debugging configuration gocommandsβ core commands:run,build,test,mod,fmt,vet, and moregofmtβ the auto-formatter that enforces a consistent code style
Your environment is ready. In the next section, we'll write our first Go program.