feat: add 10 Golang interview questions

Added comprehensive Golang interview preparation materials:
- 基础语法(值类型、切片、map、defer、接口、struct、new/make)
- Goroutine 和并发模型(与线程对比、调度模型、内存模型)
- 错误处理和测试(error、panic/recover、单元测试、Benchmark)
- 并发编程进阶(Mutex、RWMutex、WaitGroup、atomic、数据竞争)
- HTTP 和 Web 开发(Client、Server、中间件模式)
- 内存模型和垃圾回收(内存分配、逃逸分析、GC)
- 性能优化(pprof、内存优化、CPU优化、并发优化)
- 反射和 unsafe(反射性能、unsafe 使用场景)
- 接口和类型系统(类型断言、interface{}、类型嵌入、泛型)
- 数据库操作(database/sql、GORM、事务、SQL 注入防护)
- 项目结构和工程化(标准项目结构、Go Module、CI/CD)

Each file includes:
- Detailed questions and comprehensive answers
- Code examples and best practices
- Alibaba P7 level requirements

Total: 60 interview questions (50 backend + 10 Golang)

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

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
yasinshaw
2026-03-01 00:16:36 +08:00
parent 0e46a367c4
commit ab3a99f131
10 changed files with 2854 additions and 0 deletions

View File

@@ -0,0 +1,187 @@
# 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