Setting up Go correctly is the first step to productive development. This guide covers installation across different operating systems and proper environment configuration. For a broader overview of the Go ecosystem, see our Go Programming Guide.
Installing Go
macOS Installation
# Using Homebrew (recommended)
brew install go
# Verify installation
go version
# Check installation location
which go
Linux Installation
# Download latest Go version
wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz
# Extract to /usr/local
sudo tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz
# Add to PATH in ~/.bashrc or ~/.zshrc
export PATH=$PATH:/usr/local/go/bin
# Reload shell
source ~/.bashrc
# Verify installation
go version
Windows Installation
# Download MSI installer from https://go.dev/dl/
# Run the installer and follow prompts
# Verify installation (in PowerShell)
go version
# Check installation location
where go
Environment Variables
Essential Go Environment Variables
# GOROOT: Go installation directory
export GOROOT=/usr/local/go
# GOPATH: Go workspace directory
export GOPATH=$HOME/go
# Add Go binaries to PATH
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
# Verify settings
go env
Checking Environment
# View all Go environment variables
go env
# View specific variable
go env GOPATH
go env GOROOT
go env GOOS
go env GOARCH
Go Workspace Structure
Traditional GOPATH Workspace (Pre-Go 1.11)
$GOPATH/
├── bin/ # Compiled binaries
├── pkg/ # Compiled packages
└── src/ # Source code
├── github.com/
│ └── username/
│ └── project/
└── example.com/
└── package/
Modern Go Modules (Go 1.11+)
Since Go 1.11, the module system replaced GOPATH-based development. See Go Modules and Dependency Management for a deep dive.
my-project/
├── go.mod # Module definition
├── go.sum # Dependency checksums
├── main.go # Main package
├── cmd/
│ └── myapp/
│ └── main.go
├── internal/
│ └── mypackage/
│ └── package.go
├── pkg/
│ └── publicpackage/
│ └── package.go
└── tests/
└── main_test.go
Setting Up Your First Project
Creating a New Module
# Create project directory
mkdir my-go-app
cd my-go-app
# Initialize Go module
go mod init github.com/username/my-go-app
# Create main.go
cat > main.go << 'EOF'
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
EOF
# Run the program
go run main.go
# Build executable
go build
# Run executable
./my-go-app
go.mod File
module github.com/username/my-go-app
go 1.23
require (
github.com/some/package v1.2.3
)
Verifying Installation
Complete Setup Check
#!/bin/bash
echo "=== Go Installation Check ==="
# Check Go version
echo "Go version:"
go version
# Check GOROOT
echo -e "\nGOROOT:"
go env GOROOT
# Check GOPATH
echo -e "\nGOPATH:"
go env GOPATH
# Check OS and Architecture
echo -e "\nOS and Architecture:"
echo "GOOS: $(go env GOOS)"
echo "GOARCH: $(go env GOARCH)"
# Check Go binary location
echo -e "\nGo binary location:"
which go
# Test compilation
echo -e "\nTesting compilation:"
go version
echo -e "\n✓ Go installation verified!"
Troubleshooting
Common Issues
# Issue: "go: command not found"
# Solution: Add Go to PATH
export PATH=$PATH:/usr/local/go/bin
# Issue: GOPATH not set
# Solution: Set GOPATH explicitly
export GOPATH=$HOME/go
# Issue: Module not found
# Solution: Initialize module
go mod init github.com/username/project
# Issue: Dependency conflicts
# Solution: Clean and download dependencies
go clean -modcache
go mod download
Verifying PATH
# Check if Go is in PATH
echo $PATH
# Verify Go binary is accessible
go version
# Check Go binary location
which go
IDE and Editor Setup
VS Code Setup
# Install Go extension
# Open VS Code and search for "Go" in extensions
# Install the official Go extension by Google
# Install Go tools
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/go-delve/delve/cmd/dlv@latest
GoLand Setup
For a detailed GoLand configuration walkthrough, see our GoLand IDE Setup Guide.
# Download from https://www.jetbrains.com/go/
# Install and configure Go SDK path
# GoLand will auto-detect Go installation
Best Practices
✅ Good: Proper Environment Setup
# Set up environment variables in shell profile
cat >> ~/.bashrc << 'EOF'
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
EOF
source ~/.bashrc
❌ Bad: Incorrect PATH Configuration
# DON'T: Forget to add Go to PATH
# This will result in "go: command not found"
Summary
Proper Go installation and environment setup ensures:
- Correct Go version installed
- GOPATH and GOROOT properly configured
- PATH includes Go binaries
- IDE integration working correctly
- Module system ready for projects
With these foundations in place, you’re ready to start Go development! Next, explore our Go Best Practices guide for writing idiomatic Go code.
Comments