Setting Up a Go Development Environment

  1. Environment Variables

The key Go environment variables are:

  • GOROOT: Points to the Go SDK installation directory.
  • PATH: Should include $GOROOT/bin for command-line access.
  • GOPATH: Specifies your workspace directory (used in legacy GOPATH mode).

When using Go modules (GO111MODULE=on), dependencies downloaded via go get are stored under $GOPATH/pkg/mod/.

Example:

# Running in D:\00_develop\ycx-study\ycx-go\src\p1
go get golang.org/x/net/html
# Package saved to D:\00_develop\ycx-study\ycx-go\pkg\mod
  1. Default Go Environment Configuration

Use go env to inspect current settings:

go env GOOS

Sample output includes:

GOARCH=amd64
GOOS=windows
GOROOT=C:\Program Files\Go
GOPATH=D:\projects\study\go
GOMODCACHE=D:\projects\study\go\pkg\mod
GOPROXY=https://proxy.golang.org,direct
CGO_ENABLED=0
GOBIN=
GOCACHE=C:\Users\MOREFINE\AppData\Local\go-build
  1. Compilation

Basic compilation:

go build main.go
go build -o m.exe main.go

Cross-compiling for Linux AMD64:

set GOARCH=amd64
set GOOS=linux
set CGO_ENABLED=0
go build -o main_amd64 main.go

Cross-compiling for Linux ARM64:

set GOARCH=arm64
set GOARM=v8.0
set GOOS=linux
set CGO_ENABLED=0
go build -o main_arm64 main.go
  1. Configuring VS Code

Enable Go modules and set a proxy (e.g., for users in China):

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

Install the official Go extension for VS Code. Then, press Ctrl+Shift+P, type "Go: Install/Update Tools", and install all recommended tools.

To enable debugging:

  1. Navigate to your project directory (e.g., D:\00_develop\ycx-study\ycx-go\src\p1).
  2. Initialize a module: ``` go mod init github.com/ycx/p1
  3. Open the p1 folder in VS Code.
  4. Create .vscode/launch.json:
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Package",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${workspaceFolder}",
      "args": ["-config", "server.json"],
      "cwd": "${workspaceFolder}",
      "buildFlags": "-tags 'server'"
    }
  ]
}

Press F5 to start debugging, F10 to step over, and F11 too step into functions.

  1. Managing Environment Variables

PowerShell

Temporary:

$env:VAR_NAME = "value"

Permannet (user scope):

[System.Environment]::SetEnvironmentVariable("VAR_NAME", "value", "User")

Permanent (system scope):

[System.Environment]::SetEnvironmentVariable("VAR_NAME", "value", "Machine")

List all variables:

Get-ChildItem Env:
# or
gci Env:

View specific variable:

$env:Path
Get-Item Env:Path

Command Prompt (CMD)

Temporary:

set VAR_NAME=value

Permanent (user):

setx VAR_NAME value

Permanent (system):

setx /M VAR_NAME value

List all variables:

set

View specific variable:

echo %Path%

Tags: Go Golang VSCode cross-compilation environment-variables

Posted on Sun, 11 Oct 2026 16:11:02 +0000 by wacook