Go Setup Guide - CodeSwordsman

Set up Go: install on Windows, Linux and macOS, configure GOPROXY and checksum services, GOROOT/GOPATH, module projects, cross-compilation and common error fixes.

Go 1.22+Updated 2026-10-2930 commands

Install & Version Management

Install the latest stable Go on Windows; the installer sets GOROOT and PATH

winget install GoLang.Go

Use Homebrew on macOS and unpack the official tarball into /usr/local/go on Linux

# macOS
brew install go
# Ubuntu / Debian(用官方包,发行版源版本偏旧)
wget https://go.dev/dl/go1.22.6.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.22.6.linux-amd64.tar.gz

Add /usr/local/go/bin to PATH after unpacking

Check the Go version used by the CLI

go version
go env GOVERSION

Install several Go versions side by side with the official dl tool and call them by versioned prefix

go install golang.org/dl/go1.21.13@latest
go1.21.13 download

Install the aggregated linter instead of assembling individual linters

go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

Mirrors & Proxy

Switch the module proxy and checksum database; the single most important step for downloads in China

go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=sum.golang.google.cn

Bypass the public proxy and checksum for private repositories

go env -w GOPRIVATE=git.example.com/*
go env -w GONOSUMDB=git.example.com/*

Disable checksum verification when the intranet cannot reach the checksum service

go env -w GONOSUMCHECK=1
go env -w GOSUMDB=off

Disabling checks weakens supply-chain safety; only do this on a fully controlled intranet

Route go commands through a proxy while excluding intranet hosts

go env -w HTTPS_PROXY=http://127.0.0.1:7890
go env -w NO_PROXY=localhost,127.0.0.1,git.example.com

Remove a value written by go env -w and fall back to the default

go env -u GOPROXY

Environment Variables

Show the SDK root, workspace directory and binary output directory

go env GOROOT GOPATH GOBIN

GOPATH defaults to ~/go; binaries from go install land in GOPATH/bin

Add GOPATH/bin to PATH or commands installed with go install will not be callable

# Windows(永久生效)
setx PATH "%PATH%;%USERPROFILE%\go\bin"
# macOS / Linux
echo 'export PATH="$HOME/go/bin:$PATH"' >> ~/.zshrc

Set a custom binary output directory to keep tools in one place

go env -w GOBIN=C:\tools\bin

Pin module behavior so builds do not fail on a read-only go.mod

go env -w GOFLAGS=-mod=mod

Create & Verify

Initialize a module; the module path becomes the import prefix

mkdir demo && cd demo
go mod init github.com/you/demo

Tidy dependencies and run the current package

go mod tidy
go run .

Compile a single binary and run it; no runtime needed on the target host

go build -o bin/app .
./bin/app

Cross-compile for Linux; on Windows assign environment variables first

# Windows 构建 Linux 产物
$env:GOOS="linux"; $env:GOARCH="amd64"; go build -o app .
# macOS / Linux
GOOS=linux GOARCH=amd64 go build -o app .

Run all tests plus the official vet checks

go test ./...
go vet ./...

Inspect the module graph to see which dependency bumped a version

go mod graph | grep gin

IDE & Plugins

Install the official Go extension; the first open prompts to install gopls and friends

code --install-extension golang.go

Install the language server manually; retry with GOPROXY set when the proxy is restricted

go install golang.org/x/tools/gopls@latest

Install Delve, which the VS Code Go debugger depends on

go install github.com/go-delve/delve/cmd/dlv@latest

Install GoLand on Windows; use brew --cask goland on macOS

winget install JetBrains.GoLand

Troubleshooting

When go is not found, check that PATH contains the Go bin directory; reopen the terminal on Windows

# Windows
where go
# macOS / Linux
which -a go

Switch the proxy, clear the module cache and download again when timeouts occur

go env -w GOPROXY=https://goproxy.cn,direct
go clean -modcache
go mod download

Find out which import chain pulls in a dependency

go mod why -m github.com/gin-gonic/gin

Stop Go from auto-downloading a newer toolchain, which stalls offline build machines

go env -w GOTOOLCHAIN=local

Clear build and test caches when results look stale

go clean -cache -testcache

Locate the process holding the port when startup reports it is already in use

netstat -ano | findstr :8080

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.