The Makefile provides convenient build automation for the Go-API project. It includes targets for development, testing, building, and deployment tasks.
- Go 1.19+ installed
- Docker (for Docker-related targets)
- goimports tool:
go install golang.org/x/tools/cmd/goimports@latest
Runs the complete development workflow: format, test, and build.
make all
# Equivalent to: make fmt test buildFormats the source code using gofmt and organizes imports with goimports.
make fmtWhat it does:
- Formats all Go files in the current directory
- Organizes and cleans up import statements
- Ensures consistent code style
Runs all tests with verbose output.
make testFeatures:
- Runs tests in all subdirectories
- Provides verbose output for debugging
- Exits with error code if tests fail
Builds the application binary with optimizations.
make buildWhat it does:
- Runs
make fmtfirst to ensure code is formatted - Creates
./bindirectory if it doesn't exist - Builds optimized binary with
-s -wflags (strip symbols and debug info) - Output:
./bin/go-api(or custom APP_NAME)
Runs the compiled application.
make runRequirements:
- Binary must be built first with
make build - Configuration files should be in
bin/configs/
Builds the Docker image for the application.
make docker-buildFeatures:
- Uses optimized Dockerfile
- Sets timezone using TZ environment variable
- Tags image as
go-api:latest(or custom IMAGE_NAME)
Runs the application in a Docker container.
make docker-runWhat it does:
- Stops and removes existing container (via
docker-clean) - Runs new container with:
- Port mapping: 8080:8080
- Volume mount for configs:
./bin/configs:/bin/configs - Environment variables: APP_NAME, RUN_ENV
- Restart policy: always
- Detached mode
Stops and removes the existing Docker container.
make docker-cleanSafe operation:
- Gracefully stops container if running
- Removes container forcefully if needed
- Ignores errors if container doesn't exist
Removes build artifacts.
make cleanWhat it removes:
- Application binary from
./bin/directory - Keeps configuration and other files intact
You can customize the build process using environment variables:
| Variable | Default | Description |
|---|---|---|
APP_NAME |
go-api |
Application name and binary name |
TZ |
Asia/Shanghai |
Timezone for Docker container |
IMAGE_NAME |
$(APP_NAME):latest |
Docker image name and tag |
CONFIG_DIR |
$(pwd)/bin/configs |
Configuration directory path |
RUN_ENV |
local |
Runtime environment |
# Complete development cycle
make all
# Individual steps
make fmt # Format code
make test # Run tests
make build # Build binary
make run # Run application# Build with custom name
APP_NAME=my-api make build
# Run Docker with custom name
APP_NAME=my-api make docker-run# Build for production
RUN_ENV=prod make build
# Run Docker in production mode
RUN_ENV=prod make docker-run# Build with custom image name
IMAGE_NAME=my-registry/go-api:v1.0.0 make docker-build
# Run with custom image
IMAGE_NAME=my-registry/go-api:v1.0.0 make docker-run# Build Docker with different timezone
TZ=UTC make docker-buildProblem: goimports: command not found
# Solution: Install goimports
go install golang.org/x/tools/cmd/goimports@latestProblem: Binary not found when running make run
# Solution: Build first
make build
make runProblem: Permission denied accessing Docker
# Solution: Add user to docker group or use sudo
sudo make docker-build
sudo make docker-runProblem: Port 8080 already in use
# Solution: Stop existing containers or change port
make docker-clean
# Or modify Makefile to use different portProblem: Configuration files not found in container
# Solution: Ensure configs exist locally
ls -la ./bin/configs/
# Create default config if needed
cp bin/configs/local.json.default bin/configs/local.json# Run format and tests in parallel (if supported)
make -j2 fmt test# See actual commands being executed
make -n build
# Verbose make output
make -d build# GitHub Actions example
- name: Build and Test
run: |
make all
make docker-buildYou can extend the Makefile for your specific needs:
# Add custom target
deploy: docker-build
@echo "Deploying application..."
@docker push $(IMAGE_NAME)
# Add linting
lint:
@golangci-lint run ./...
# Add coverage
coverage:
@go test -coverprofile=coverage.out ./...
@go tool cover -html=coverage.outMakefile 为 Go-API 项目提供了便捷的构建自动化。它包括用于开发、测试、构建和部署任务的目标。
- 安装 Go 1.19+
- Docker(用于 Docker 相关目标)
- goimports 工具:
go install golang.org/x/tools/cmd/goimports@latest
运行完整的开发工作流程:格式化、测试和构建。
make all
# 等同于:make fmt test build使用 gofmt 格式化源代码,并使用 goimports 整理导入。
make fmt功能:
- 格式化当前目录中的所有 Go 文件
- 整理和清理导入语句
- 确保一致的代码风格
运行所有测试并提供详细输出。
make test特性:
- 在所有子目录中运行测试
- 提供详细输出用于调试
- 如果测试失败则退出并返回错误代码
构建优化的应用程序二进制文件。
make build功能:
- 首先运行
make fmt确保代码格式化 - 如果不存在则创建
./bin目录 - 使用
-s -w标志构建优化的二进制文件(去除符号和调试信息) - 输出:
./bin/go-api(或自定义 APP_NAME)
运行编译的应用程序。
make run要求:
- 必须先使用
make build构建二进制文件 - 配置文件应该在
bin/configs/目录中
为应用程序构建 Docker 镜像。
make docker-build特性:
- 使用优化的 Dockerfile
- 使用 TZ 环境变量设置时区
- 标记镜像为
go-api:latest(或自定义 IMAGE_NAME)
在 Docker 容器中运行应用程序。
make docker-run功能:
- 停止并移除现有容器(通过
docker-clean) - 运行新容器,具有:
- 端口映射:8080:8080
- 配置卷挂载:
./bin/configs:/bin/configs - 环境变量:APP_NAME、RUN_ENV
- 重启策略:always
- 分离模式
停止并移除现有的 Docker 容器。
make docker-clean安全操作:
- 如果容器正在运行则优雅停止
- 如果需要则强制移除容器
- 如果容器不存在则忽略错误
移除构建产物。
make clean移除内容:
./bin/目录中的应用程序二进制文件- 保持配置和其他文件不变
您可以使用环境变量自定义构建过程:
| 变量 | 默认值 | 描述 |
|---|---|---|
APP_NAME |
go-api |
应用程序名称和二进制文件名 |
TZ |
Asia/Shanghai |
Docker 容器的时区 |
IMAGE_NAME |
$(APP_NAME):latest |
Docker 镜像名称和标签 |
CONFIG_DIR |
$(pwd)/bin/configs |
配置目录路径 |
RUN_ENV |
local |
运行时环境 |
# 完整开发周期
make all
# 单独步骤
make fmt # 格式化代码
make test # 运行测试
make build # 构建二进制文件
make run # 运行应用程序# 使用自定义名称构建
APP_NAME=my-api make build
# 使用自定义名称运行 Docker
APP_NAME=my-api make docker-run# 为生产环境构建
RUN_ENV=prod make build
# 在生产模式下运行 Docker
RUN_ENV=prod make docker-run问题:goimports: command not found
# 解决方案:安装 goimports
go install golang.org/x/tools/cmd/goimports@latest问题:运行 make run 时找不到二进制文件
# 解决方案:先构建
make build
make run问题:访问 Docker 权限被拒绝
# 解决方案:将用户添加到 docker 组或使用 sudo
sudo make docker-build
sudo make docker-run问题:端口 8080 已被使用
# 解决方案:停止现有容器或更改端口
make docker-clean
# 或修改 Makefile 使用不同端口您可以根据特定需求扩展 Makefile:
# 添加自定义目标
deploy: docker-build
@echo "部署应用程序..."
@docker push $(IMAGE_NAME)
# 添加代码检查
lint:
@golangci-lint run ./...
# 添加覆盖率检查
coverage:
@go test -coverprofile=coverage.out ./...
@go tool cover -html=coverage.out