Skip to main content

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

  1. Go to https://go.dev/dl/
  2. Download go1.24.0.darwin-amd64.pkg (Intel) or go1.24.0.darwin-arm64.pkg (Apple Silicon)
  3. Run the PKG file and follow the prompts
  4. Open a new terminal and run go version to confirm

Windows​

Method 1: MSI installer (recommended)

  1. Go to https://go.dev/dl/
  2. Download go1.24.0.windows-amd64.msi
  3. Run the MSI file and follow the installer
  4. 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:

VariableDescriptionDefault
GOROOTGo standard library installation path/usr/local/go
GOPATHGo workspace path (module cache, etc.)~/go
GOMODCACHEDownloaded module cache location$GOPATH/pkg/mod
GOBINBinary install path for go install$GOPATH/bin
GOOSTarget OS for buildsCurrent OS
GOARCHTarget architecture for buildsCurrent architecture
GOPROXYModule download proxyhttps://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.

~/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 get always 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​

  1. Open VS Code
  2. Open the Extensions Marketplace (Ctrl+Shift+X)
  3. Search for "Go" and install Go (by Go Team at Google)
  4. After restarting VS Code, open any .go file and it will prompt you to install gopls

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
{
"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.

  1. Download from https://www.jetbrains.com/go/
  2. After installing, set the Go SDK path under Settings β†’ Go β†’ GOROOT
  3. Verify that module mode is enabled under Settings β†’ Go β†’ Go Modules
  4. Free licenses available for students and open source projects

Complete go Command Reference​

CommandDescriptionExample
go runCompile and run source files immediatelygo run main.go
go buildCompile the current package to a binarygo build -o app ./...
go testRun test files (_test.go)go test ./...
go mod initInitialize a new modulego mod init github.com/user/proj
go mod tidyRemove unused dependencies, add missing onesgo mod tidy
go getAdd or update a package at a specific versiongo get github.com/gin-gonic/gin@v1.9.1
go installInstall a package as a binary to GOBINgo install github.com/air-verse/air@latest
go fmtAuto-format code (wrapper around gofmt)go fmt ./...
go vetStatic analysis for potential bugsgo vet ./...
go docPrint package/function documentationgo doc fmt.Println
go envPrint Go environment variablesgo env GOPATH
go cleanRemove build cache and generated filesgo clean -cache
go listList packagesgo list ./...
go workManage 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, and GOPROXY
  • Go Modulesβ€” the standard dependency management approach for modern Go projects
  • VS Code setupβ€” gopls, auto-formatting, debugging configuration
  • go commandsβ€” core commands: run, build, test, mod, fmt, vet, and more
  • gofmtβ€” the auto-formatter that enforces a consistent code style

Your environment is ready. In the next section, we'll write our first Go program.