Go Installation and Environment Setup
Setting up Go correctly is the first step to productive development. This guide covers installation across different operating systems and proper environment configuration.
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.21.0.linux-amd64.tar.gz
# Extract to /usr/local
sudo tar -C /usr/local -xzf go1.21.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+)
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.21
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
# 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!
Comments