Skip to content

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

Windows

  1. Download Installation Package

    • Visit Go Official Website
    • Download the appropriate Windows installer (.msi file)
    • Example: go1.21.0.windows-amd64.msi
  2. Run Installer

    Double-click .msi file → Follow wizard to complete installation
    Default installation path: C:\Program Files\Go
  3. Verify Installation

    cmd
    # Open command prompt
    go version
    # Should display: go version go1.21.0 windows/amd64

macOS

  1. 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)
  2. 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 go
  3. Verify Installation

    bash
    go version
    # Should display: go version go1.21.0 darwin/amd64

Linux

  1. 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.gz
  2. Configure Environment Variables

    bash
    # Edit ~/.bashrc or ~/.zshrc
    echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
    
    # Reload configuration
    source ~/.bashrc
  3. Verify Installation

    bash
    go version
    # Should display: go version go1.21.0 linux/amd64

Method Two: Package Manager Installation

Ubuntu/Debian

bash
# Install using apt
sudo apt update
sudo apt install golang-go

# Or install specific version
sudo apt install golang-1.21

CentOS/RHEL/Fedora

bash
# CentOS/RHEL
sudo yum install golang

# Fedora
sudo dnf install golang

Arch Linux

bash
# Install using pacman
sudo pacman -S go

⚙️ Environment Variable Configuration

Important Environment Variables

VariableDescriptionDefault ValueExample
GOROOTGo installation directoryAuto-detected/usr/local/go
GOPATHWorkspace path$HOME/go/home/user/go
GOPROXYModule proxyhttps://proxy.golang.orgDomestic mirror
GOBINExecutable installation path$GOPATH/binCustom path

Windows Configuration

  1. Open System Environment Variables

    Control Panel → System → Advanced System Settings → Environment Variables
  2. Add Environment Variables

    cmd
    # System variables (recommended)
    GOROOT=C:\Program Files\Go
    GOPATH=C:\Users\your-username\go
    
    # Add to PATH
    %GOROOT%\bin
    %GOPATH%\bin
  3. Verify Configuration

    cmd
    echo %GOROOT%
    echo %GOPATH%
    go env

macOS/Linux Configuration

  1. Edit Configuration File

    bash
    # bash users
    nano ~/.bashrc
    
    # zsh users
    nano ~/.zshrc
  2. Add 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.cn
  3. Apply Configuration

    bash
    source ~/.bashrc  # or ~/.zshrc
  4. Verify Configuration

    bash
    echo $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

Install VS Code

bash
# Download from official website: https://code.visualstudio.com/
# Or install using package manager

Install Go Extension

  1. Open VS Code
  2. Press Ctrl+Shift+X to open extensions marketplace
  3. Search and install "Go" extension (Google official)
  4. Restart VS Code

Configure Go Tools

bash
# In VS Code, press Ctrl+Shift+P
# Type: Go: Install/Update Tools
# Select all tools and install

VS Code Configuration File

json
// 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

bash
# Download from official website: https://www.jetbrains.com/go/
# 30-day free trial, free for students

3. Vim/Neovim

vim-go Plugin

bash
# 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

bash
# 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 test

Useful Third-Party Tools

Code Formatting

bash
# Install goimports
go install golang.org/x/tools/cmd/goimports@latest

# Use
goimports -w *.go

Code Linting

bash
# Install golint
go install golang.org/x/lint/golint@latest

# Use
golint ./...

Dependency Management

bash
# 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

bash
mkdir hello-go
cd hello-go

2. Initialize Module

bash
go mod init hello-go

3. Create Main Program

go
// main.go
package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
    fmt.Println("Welcome to learning Go!")
}

4. Run Program

bash
# Method 1: Run directly
go run main.go

# Method 2: Compile and run
go build
./hello-go      # Linux/macOS
hello-go.exe    # Windows

5. Project Structure

hello-go/
├── go.mod
├── main.go
└── hello-go        # Generated executable

🌐 Configure Network Proxy (Chinese Users)

Go Module Proxy

bash
# 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.cn

Verify Proxy Configuration

bash
# 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

bash
# 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,direct

2. Permission Issues (Linux/macOS)

bash
# Ensure write permissions
chmod -R 755 $GOPATH

# Change owner
sudo chown -R $USER:$USER $GOPATH

3. Network Issues

bash
# 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/goimports

4. Version Conflicts

bash
# 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 version shows version info)
  • [ ] Environment variables correctly configured (go env shows 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.

Content is for learning and research only.