35 lines
2.0 KiB
Markdown
Raw Permalink Normal View History

## 使用 Dev Container 开发 demo 工程
此目录下的 `demo/` 是一个 Go 工程。已提供 VS Code Dev Container 配置,支持在容器内进行开发与调试。
### 快速开始
- 前置:本地安装 VS Code 与 Dev Containers 扩展Microsoft 提供)。
- 打开文件夹:在 VS Code 中打开 `day01/devcontainer` 目录。
- 重新在容器中打开:命令面板运行 “Dev Containers: Reopen in Container”。
- 首次启动会执行依赖下载:`postCreateCommand` 会在 `demo/` 下执行 `go mod download`
- 当前使用 `ghcr.io/devcontainers/base:ubuntu` 基础镜像,并通过 Dev Containers Feature 安装 Go1.25)。若网络再遇阻,可切换到本地 Dockerfile 方案作为备用。
### 容器内开发
- 代码位置:`/workspaces/<当前打开的文件夹>/demo`
- 常用操作:
- 运行:在容器终端进入 `demo/` 目录后执行 `go run main.go`
- 构建:`go build`
- 测试:`go test ./...`
- 已安装Go 工具链与 VS Code Go 扩展;保存时自动格式化与常用 lint 设置。
### 调试
- 已自动安装 Delve`dlv`)。
- VS Code 已提供调试配置:[day01/devcontainer/.vscode/launch.json](day01/devcontainer/.vscode/launch.json)
- 直接在 “Run and Debug” 视图选择 “Debug demo/main.go” 或 “Debug demo tests”。
- 断点与变量查看可在容器内正常使用。
### 预装 VS Code Server可选避免下载卡顿
- 如需避免容器运行时下载 VS Code Server可切换到本地 Dockerfile 方案并在构建期预装 Server见 [day01/devcontainer/.devcontainer/Dockerfile](day01/devcontainer/.devcontainer/Dockerfile))。
- 也可以在宿主机下载后手动注入到容器:将 server 包复制到 `~/.vscode-server/bin/<commit>` 目录。
### 备注
- 如需额外工具(如 `golangci-lint` 的安装或 `dlv` 调试器),可在 `.devcontainer/Dockerfile``postCreateCommand` 中添加。
# devcontainer demo 工程
针对简单场景: 直接用 git bash 做完 vscode的命令工具 来使用