Go Development Environment
Before starting to learn Go, we need to set up a complete development environment. This chapter will detail how to install and configure the Go development environment on different operating systems.
🛠️ System Requirements
Supported Operating Systems
- Windows: Windows 10 or higher
- macOS: macOS 10.15 or higher
- Linux: Major distributions (Ubuntu, CentOS, Debian, etc.)
- FreeBSD: FreeBSD 12 or higher
Hardware Requirements
- CPU: x86-64 or ARM64 architecture
- Memory: Minimum 2GB RAM, 4GB or more recommended
- Disk Space: At least 500MB available space
📥 Installing Go
Method One: Official Installation Package (Recommended)
Windows
Download Installation Package
- Visit Go Official Website
- Download the appropriate Windows installer (.msi file)
- Example:
go1.21.0.windows-amd64.msi
Run Installer
Double-click .msi file → Follow wizard to complete installation Default installation path: C:\Program Files\GoVerify Installation
cmd# Open command prompt go version # Should display: go version go1.21.0 windows/amd64
macOS
Download Installation Package
bash# Method 1: Download .pkg file from official website # Visit https://golang.org/dl/ # Download: go1.21.0.darwin-amd64.pkg (Intel Mac) # Or: go1.21.0.darwin-arm64.pkg (Apple Silicon Mac)Install Using Homebrew (Recommended)
bash# Install Homebrew (if not installed) /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # Install Go using Homebrew brew install goVerify Installation
bashgo version # Should display: go version go1.21.0 darwin/amd64
Linux
Download and Extract
bash# Download Go installation package wget https://golang.org/dl/go1.21.0.linux-amd64.tar.gz # Remove old version (if exists) sudo rm -rf /usr/local/go # Extract to /usr/local sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gzConfigure Environment Variables
bash# Edit ~/.bashrc or ~/.zshrc echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc # Reload configuration source ~/.bashrcVerify Installation
bashgo version # Should display: go version go1.21.0 linux/amd64
Method Two: Package Manager Installation
Ubuntu/Debian
# Install using apt
sudo apt update
sudo apt install golang-go
# Or install specific version
sudo apt install golang-1.21CentOS/RHEL/Fedora
# CentOS/RHEL
sudo yum install golang
# Fedora
sudo dnf install golangArch Linux
# Install using pacman
sudo pacman -S go⚙️ Environment Variable Configuration
Important Environment Variables
| Variable | Description | Default Value | Example |
|---|---|---|---|
GOROOT | Go installation directory | Auto-detected | /usr/local/go |
GOPATH | Workspace path | $HOME/go | /home/user/go |
GOPROXY | Module proxy | https://proxy.golang.org | Domestic mirror |
GOBIN | Executable installation path | $GOPATH/bin | Custom path |
Windows Configuration
Open System Environment Variables
Control Panel → System → Advanced System Settings → Environment VariablesAdd Environment Variables
cmd# System variables (recommended) GOROOT=C:\Program Files\Go GOPATH=C:\Users\your-username\go # Add to PATH %GOROOT%\bin %GOPATH%\binVerify Configuration
cmdecho %GOROOT% echo %GOPATH% go env
macOS/Linux Configuration
Edit Configuration File
bash# bash users nano ~/.bashrc # zsh users nano ~/.zshrcAdd Environment Variables
bash# Go environment variables export GOROOT=/usr/local/go export GOPATH=$HOME/go export GOBIN=$GOPATH/bin export PATH=$PATH:$GOROOT/bin:$GOBIN # Configure domestic proxy (optional) export GOPROXY=https://goproxy.cn,direct export GOSUMDB=sum.golang.google.cnApply Configuration
bashsource ~/.bashrc # or ~/.zshrcVerify Configuration
bashecho $GOROOT echo $GOPATH go env
🏗️ Workspace Structure
Traditional GOPATH Mode
$GOPATH/
├── bin/ # Executable files
├── pkg/ # Compiled package files
└── src/ # Source code
└── github.com/
└── username/
└── project/Modern Module Mode (Go 1.11+)
Project Directory/
├── go.mod # Module definition file
├── go.sum # Dependency version lock
├── main.go # Main program file
├── pkg/ # Internal packages
└── vendor/ # Dependency cache (optional)🛠️ Development Tool Selection
1. Visual Studio Code (Recommended)
Install VS Code
# Download from official website: https://code.visualstudio.com/
# Or install using package managerInstall Go Extension
- Open VS Code
- Press
Ctrl+Shift+Xto open extensions marketplace - Search and install "Go" extension (Google official)
- Restart VS Code
Configure Go Tools
# In VS Code, press Ctrl+Shift+P
# Type: Go: Install/Update Tools
# Select all tools and installVS Code Configuration File
// settings.json
{
"go.gopath": "/path/to/your/gopath",
"go.goroot": "/usr/local/go",
"go.formatTool": "goimports",
"go.lintTool": "golint",
"go.vetOnSave": "package",
"go.buildOnSave": "workspace",
"go.testOnSave": true
}2. GoLand (JetBrains)
Features
- Powerful IDE
- Intelligent code completion
- Debugging and testing tools
- Version control integration
Installation
# Download from official website: https://www.jetbrains.com/go/
# 30-day free trial, free for students3. Vim/Neovim
vim-go Plugin
# Install vim-plug
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
# Add to .vimrc
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }4. Other Editors
- Sublime Text: GoSublime plugin
- Atom: go-plus plugin
- Emacs: go-mode
🔧 Go Toolchain
Basic Commands
# Check Go version
go version
# View environment information
go env
# Compile and run
go run main.go
# Compile to executable
go build
# Install to GOBIN
go install
# Download dependencies
go mod download
# Format code
go fmt
# Code analysis
go vet
# Run tests
go testUseful Third-Party Tools
Code Formatting
# Install goimports
go install golang.org/x/tools/cmd/goimports@latest
# Use
goimports -w *.goCode Linting
# Install golint
go install golang.org/x/lint/golint@latest
# Use
golint ./...Dependency Management
# Initialize module
go mod init project-name
# Tidy dependencies
go mod tidy
# View dependency graph
go mod graph🚀 Creating Your First Go Program
1. Create Project Directory
mkdir hello-go
cd hello-go2. Initialize Module
go mod init hello-go3. Create Main Program
// main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
fmt.Println("Welcome to learning Go!")
}4. Run Program
# Method 1: Run directly
go run main.go
# Method 2: Compile and run
go build
./hello-go # Linux/macOS
hello-go.exe # Windows5. Project Structure
hello-go/
├── go.mod
├── main.go
└── hello-go # Generated executable🌐 Configure Network Proxy (Chinese Users)
Go Module Proxy
# Configure Qiniu cloud proxy
go env -w GOPROXY=https://goproxy.cn,direct
# Configure Aliyun proxy
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
# Configure private modules to skip proxy
go env -w GOPRIVATE=*.corp.example.com
# Configure checksum database
go env -w GOSUMDB=sum.golang.google.cnVerify Proxy Configuration
# Test downloading package
go get golang.org/x/tools/cmd/goimports
# View proxy configuration
go env GOPROXY
go env GOSUMDB🔍 Common Issues Troubleshooting
1. Environment Variable Issues
# Check if Go is in PATH
which go
# Check all environment variables
go env
# Reset environment variables
go env -w GOPATH=$HOME/go
go env -w GOPROXY=https://proxy.golang.org,direct2. Permission Issues (Linux/macOS)
# Ensure write permissions
chmod -R 755 $GOPATH
# Change owner
sudo chown -R $USER:$USER $GOPATH3. Network Issues
# Test network connectivity
curl https://golang.org/dl/
# Use proxy
export https_proxy=http://proxy.example.com:8080
go get -x golang.org/x/tools/cmd/goimports4. Version Conflicts
# Uninstall old version
sudo rm -rf /usr/local/go
# Check for multiple Go installations
ls -la /usr/bin/go*
ls -la /usr/local/bin/go*📋 Environment Check List
Before continuing with subsequent learning, ensure the following are properly configured:
- [ ] Go successfully installed (
go versionshows version info) - [ ] Environment variables correctly configured (
go envshows correct paths) - [ ] Development tools installed with Go extension configured
- [ ] Network proxy configured (Chinese users)
- [ ] First Go program created and run
- [ ] Go toolchain commands working normally
🎓 Summary
In this chapter, we completed the full setup of the Go development environment:
- ✅ Install Go: Through official installer or package manager
- ✅ Configure Environment: Set necessary environment variables
- ✅ Choose Tools: Install and configure development tools
- ✅ Workspace: Understand Go's project structure
- ✅ First Program: Create and run Hello World
- ✅ Network Optimization: Configure proxy to speed up downloads
A good development environment is the foundation for efficient learning and development. Now you have a complete Go development environment and can start learning Go's core concepts.
Next, let's learn about Go Command Line Tools to master the basic Go toolchain usage.
Tips
It's recommended to document the development environment configuration steps so they can be quickly reproduced when setting up on other machines. Also, regularly update Go version to get the latest features and performance improvements.