Install required tools π¦
# Check Go version
go version
# Install the Delve debugger
go install github.com/go-delve/delve/cmd/dlv@latest
# Check DevSpace version
devspace version
# Install VSCode Go extensions
code --install-extension golang.go
code --install-extension devspace.devspace
Project structure design π
.
βββ cmd
β βββ main.go
βββ devspace.yaml
βββ Dockerfile.dev
βββ Dockerfile.prod
βββ go.mod
βββ go.sum
βββ internal
β βββ handler.go
β βββ model.go
β βββ service.go
βββ k8s
β βββ dev
β βββ deployment.yaml
β βββ service.yaml
βββ main
βββ start-dev.sh
Prepare the Go application π
First, build a simple but practical Go application with a few API endpoints so you can test breakpoints.
To debug Go, besides the API port you also need SSH port 22 and the Delve debugger port 40000.
Dockerfile.dev - Development container config
This file builds a container environment dedicated to development and debugging.
FROM golang:1.24
# π οΈ Install development tools
# - git: version control
# - curl/wget: network tools for API tests
# - vim/nano: edit files inside the container
# - openssh-server: SSH access
# - build-essential: build toolchain
RUN apt-get update && apt-get install -y \
git \
curl \
wget \
vim \
nano \
htop \
openssh-server \
sudo \
build-essential \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# π§ Install the latest Delve debugger
# Delve is the official Go debugger and supports remote debugging
RUN go install github.com/go-delve/delve/cmd/dlv@v1.23.1
# π€ Create a dev user (avoid running as root)
RUN useradd -m -s /bin/bash developer && \
echo "developer:devspace123" | chpasswd && \
usermod -aG sudo developer
# π Configure SSH service (remote access)
RUN mkdir -p /var/run/sshd && \
echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config && \
echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config && \
sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config
# π Set working directory
WORKDIR /app
# π¦ Copy dependency files and download
COPY go.mod go.sum ./
RUN go mod download
# π Copy source code
COPY . .
# π¨ Build a debug binary
# -gcflags="all=-N -l" disables optimizations and keeps debug info
RUN go build -gcflags="all=-N -l" -o main cmd/main.go
# π Expose ports
# 8080: app service
# 40000: Delve debugger
# 22: SSH service
EXPOSE 8080 40000 22
# π Copy startup script
COPY start-dev.sh /app/start-dev.sh
RUN chmod +x /app/start-dev.sh
# π― Default command
CMD ["/app/start-dev.sh"]
π start-dev.sh - Development startup script
This script orchestrates the startup flow of the development environment.
#!/bin/bash
set -e # Exit immediately on error
echo "π Starting development environment..."
# π Start SSH service (remote access)
echo "π Starting SSH service..."
service ssh start
# π Check Delve version (make sure the debugger is available)
echo "π Delve version:"
dlv version
# π¨ Rebuild the application
echo "π¨ Building application..."
go build -gcflags="all=-N -l" -o main cmd/main.go
# π Start the Delve debugger
# --listen=:40000: listen on port 40000
# --headless=true: no GUI
# --api-version=2: use API v2 (DAP support)
# --accept-multiclient: allow multiple clients
# --continue: continue running after start
# --log: enable logs
echo "π Starting Delve debugger on port 40000..."
dlv --listen=:40000 --headless=true --api-version=2 --accept-multiclient --continue --log exec ./main &
# β³ Wait for the debugger to start
sleep 5
echo "β
Delve debugger started successfully!"
# π Show service info
echo "π Application available on port 8080"
echo "π SSH available on port 22 (user: developer, pass: devspace123)"
echo "π Debug port available on 40000"
# π Check port status
echo "π Checking ports..."
netstat -tlnp | grep -E "(8080|40000)" || echo "Ports not ready yet"
echo "π± Development environment ready!"
# π Keep the container running
tail -f /dev/null
βοΈ devspace.yaml - DevSpace configuration
This file defines the full configuration for the development environment.
version: v2beta1
name: go-debug-demo
# πΌοΈ Image config: define how to build the container image
images:
app:
image: go-debug-demo
dockerfile: ./Dockerfile.dev # Use the dev Dockerfile
tags:
- dev-${DEVSPACE_RANDOM} # Dynamic tag to avoid cache issues
# π Deployment config: define how to deploy to Kubernetes
deployments:
app:
kubectl:
manifests:
- k8s/dev/deployment.yaml
- k8s/dev/service.yaml
# π Dev config: define dev-mode behavior
dev:
app:
# π― Select the Pod to attach to
labelSelector:
app: go-debug-demo
# π File sync: sync local changes into the container
sync:
- path: ./
container: /app
excludePaths:
- .git/
- .devspace/
- Dockerfile*
# π Port forwarding: map container ports to local machine
ports:
- port: "8080:8080" # App service
- port: "40000:40000" # Delve debugger
- port: "2222:22" # SSH service
# π₯οΈ Terminal: open an interactive terminal
terminal:
command: ./start-dev.sh
# π― Variables
vars:
- name: DEVSPACE_RANDOM
source: none
ποΈ VSCode launch.json - Debugger config
This file tells VSCode how to connect to the remote debugger.
{
"version": "0.2.0",
"configurations": [
{
"name": "π DevSpace Remote Debug",
"type": "go", // Use Go debugger
"request": "attach", // Attach to an existing process (instead of launching a new one)
"mode": "remote", // Remote debugging mode
"remotePath": "/app", // Code path in the container
"port": 40000, // Delve debugger port
"host": "127.0.0.1", // Connect to local (port forwarding)
"apiVersion": 2, // Use Delve API v2
// π Path mapping: local path <-> container path
"substitutePath": [
{
"from": "${workspaceFolder}", // VSCode workspace path
"to": "/app" // Container path
}
],
// π Debugger logging
"showLog": true,
"logOutput": "debugger",
// βοΈ Delve load config
"dlvLoadConfig": {
"followPointers": true,
"maxVariableRecurse": 1,
"maxStringLen": 64,
"maxArrayValues": 64,
"maxStructFields": -1
}
}
]
}
Start and test π§ͺ
# 1οΈβ£ Start the development environment
devspace dev
# 2οΈβ£ Set breakpoints in VSCode
# - Open cmd/main.go
# - Click the first line of healthHandler to set a breakpoint
# 3οΈβ£ Start the debugger
# - Press F5 or click "π DevSpace Remote Debug" in the debug panel
# 4οΈβ£ Trigger the breakpoint
curl http://localhost:8080/health