Files
interview/questions/13-Golang语言/项目结构和工程化.md
yasinshaw 7f3ab362b3 feat: rename Golang files to Chinese and supplement root files
Changes:
- Renamed all 10 Golang files from English to Chinese names
- Created 00-项目概述/项目概述.md with comprehensive project overview
- Created 08-算法与数据结构/算法与数据结构学习指南.md with detailed learning guide
- Created 12-面试技巧/面试准备进度.md with progress tracking
- Added .obsidian configuration for better markdown editing
- Updated Claude.MD with Chinese filename rule

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-03-01 00:33:32 +08:00

188 lines
3.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Golang 项目结构和工程化
## 问题
1. Go 的标准项目结构是怎样的?
2. Go Module 是如何管理的?
3. Go 的依赖管理有哪些最佳实践?
4. 如何构建和发布 Go 应用?
5. Go 的 CI/CD 最佳实践是什么?
---
## 标准答案
### 1. 标准项目结构
#### **Standard Go Project Layout**
```
myapp/
├── cmd/ # 主程序入口
│ └── myapp/
│ └── main.go
├── internal/ # 私有应用代码(不可导入)
│ ├── app/
│ ├── model/
│ └── service/
├── pkg/ # 公共库(可外部导入)
│ └── util/
├── api/ # API 协议定义
│ └── proto/
├── web/ # Web 静态资源
├── configs/ # 配置文件
├── scripts/ # 构建、安装脚本
├── test/ # 额外测试数据
├── docs/ # 文档
├── tools/ # 工具
├── go.mod
├── go.sum
├── Makefile
└── README.md
```
---
### 2. Go Module
#### **初始化 Module**
```bash
go mod init github.com/user/myapp
```
---
#### **go.mod 文件**
```go
module github.com/user/myapp
go 1.21
require (
github.com/gin-gonic/gin v1.9.1
github.com/spf13/viper v1.18.2
)
```
---
### 3. 依赖管理
#### **添加依赖**
```bash
go get github.com/gin-gonic/gin@latest
go get github.com/spf13/viper@v1.18.2
```
---
#### **升级依赖**
```bash
# 升级所有依赖
go get -u ./...
# 升级特定依赖
go get -u github.com/gin-gonic/gin
```
---
#### **清理未使用的依赖**
```bash
go mod tidy
```
---
### 4. 构建和发布
#### **构建应用**
```bash
# 构建
go build -o bin/myapp ./cmd/myapp
# 交叉编译
GOOS=linux GOARCH=amd64 go build -o bin/myapp-linux ./cmd/myapp
```
---
#### **Docker 多阶段构建**
```dockerfile
# 构建
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o myapp ./cmd/myapp
# 运行
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/myapp .
CMD ["./myapp"]
```
---
### 5. CI/CD 最佳实践
#### **GitHub Actions**
```yaml
name: CI
on:
push:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Install dependencies
run: go mod download
- name: Run tests
run: go test -v -race -coverprofile=coverage.out ./...
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage.out
```
---
### 6. 阿里 P7 加分项
**深度理解**
- 理解 Go 的模块管理Go Modules
- 理解 Go 的依赖管理go.mod、go.sum
- 理解 Go 的构建和发布流程
**实战经验**
- 有管理大型 Go 项目结构的经验
- 有设计 Go Module 依赖的经验
- 有构建和发布 Go 应用的经验
**工程化**
- 理解如何组织 Go 项目结构
- 理解如何进行依赖管理
- 理解如何设置 CI/CD