在過去兩週,我們從 go mod init
開始,搭起專案骨架、加上測試框架、中介層、API、pprof、metrics,甚至打通本機 Elasticsearch。今天,我們要做的是:
✅ 清理專案目錄,避免日後難以維護
✅ 補上缺的測試,讓 coverage 基本過關
✅ 撰寫 README,方便未來自己與他人快速上手
目前專案可能長這樣:
.
├── cmd/
│ └── server/ # main.go
├── internal/
│ ├── handler/ # HTTP handler
│ ├── search/ # SearchService 介面 + fake + es
│ ├── middleware/ # logging, recovery
│ └── metrics/ # Prometheus metrics
├── pkg/ # 可共用的 util
├── go.mod
├── go.sum
└── docker-compose.yml
這樣分層的好處:
cmd/
:只放啟動程式internal/
:核心邏輯(不可被外部 import)pkg/
:通用工具(例如重試策略)目標不是 100% coverage,而是把核心流程測起來:
/healthz
回傳 200/search
在 FakeSearchService 下能回傳固定資料func TestHealthz(t *testing.T) {
req := httptest.NewRequest("GET", "/healthz", nil)
w := httptest.NewRecorder()
HealthzHandler(w, req)
if w.Result().StatusCode != http.StatusOK {
t.Errorf("expected 200, got %d", w.Result().StatusCode)
}
}
最後,把這兩週的工作整理成 README:
/healthz
and /search
git clone https://github.com/USERNAME/cloud-native-search.git
cd cloud-native-search
docker compose up -d es01
go run ./cmd/server
Open http://localhost:8080/search?q=golang
go test ./...
Phase 2: Elasticsearch — 安裝、mapping、bulk 匯入、調優。
今天我們做了「穩定化」:
到這裡,Golang Phase(Day 2–15)完成
接下來,我們將進入 Elasticsearch Phase,正式深入 ES 的 mapping、查詢與效能調優。