字
字节笔记本
2026年2月22日
18F/golang-api-backend - Go API 后端开发模板项目
本文介绍 18F 开源的 golang-api-backend 项目,一个 Go 语言 API 后端开发模板。该项目提供了完整的 Docker 容器化开发环境,帮助开发者快速搭建云原生 Web 服务。
项目简介
golang-api-backend(又名 hello-ampd)是由 18F(美国联邦政府数字服务机构)开发维护的开源项目。该项目是一个基础的 Go API 后端模板,提供 "Hello World" 风格的 HTTP GET 响应,适用于教学演示和快速原型开发。
项目特点:
- 使用 Docker 和 docker-compose 提供自包含的构建和测试环境
- 支持本地运行和云部署(cloud.gov)
- 包含完整的 CI/CD 流程(CircleCI)
- 集成代码质量检查(Code Climate、Go Report Card、Codecov)
docker-compose.yml 详解
该项目使用 docker-compose 定义服务配置,实现开发环境和生产环境的一致性:
yaml
# This docker service serves 2 purposes:
# - a self-contained Go build environment
# - a local execution environment
version: '2'
services:
api:
build:
# path to the Dockerfile -- contains the commands/description of
# how to assemble the base image used for this service
context: .
dockerfile: etc/docker/api.Dockerfile
env_file:
# used to set key/value pairs that set the environment variables
# for the execution context of the command run in the Dockerfile
- .env
volumes:
# mount the current directory into the container so we can edit the
# source with tools in the host OS, but build them in the container
- .:/go/src/github.com/18F/hello-ampd
# create `vendor` as a separate volume so the host OS mount above
# doesn't mask/overlay the true vendor tree which is populated in
# the Dockerfile
- /go/src/github.com/18F/hello-ampd/src/vendor
ports:
# map port 8080 on the host OS to the port defined in .env
- "8080:${PORT}"配置要点
| 配置项 | 说明 |
|---|---|
build.context | 构建上下文路径,指向项目根目录 |
build.dockerfile | Dockerfile 路径:etc/docker/api.Dockerfile |
env_file | 环境变量配置文件 .env |
volumes | 挂载当前目录到容器,实现热更新开发 |
ports | 将主机 8080 端口映射到容器内 ${PORT} 变量定义的端口 |
技术栈
- Go (42.5%) - 主要开发语言
- Shell (36.1%) - 构建脚本和工具
- Dockerfile (16.2%) - 容器化配置
- Makefile (5.2%) - 构建自动化
快速开始
前置要求
- Git
- Docker
- docker-compose
克隆仓库
bash
git clone https://github.com/18F/hello-ampd
cd hello-ampd运行测试
bash
docker-compose run --rm api make启动本地服务
bash
docker-compose up --abort-on-container-exit --build服务启动后,访问 http://localhost:8080 即可。
停止服务
bash
docker-compose down项目结构
text
.
├── .circleci/ # CI/CD 配置
├── bin/ # 脚本工具
├── docs/ # 文档
├── etc/docker/ # Docker 配置
├── src/ # Go 源代码
├── .env # 环境变量
├── docker-compose.yml # Docker Compose 配置
└── README.md # 项目说明项目状态
⚠️ 注意:该项目已于 2020 年 1 月 17 日被归档,现为只读状态。
项目链接
分享: