33 lines
709 B
Markdown
33 lines
709 B
Markdown
|
|
---
|
|||
|
|
paths:
|
|||
|
|
- "**/*.go"
|
|||
|
|
- "**/go.mod"
|
|||
|
|
- "**/go.sum"
|
|||
|
|
---
|
|||
|
|
# Go 程式碼風格 (Coding Style)
|
|||
|
|
|
|||
|
|
> 本檔案擴展了 [common/coding-style.md](../common/coding-style.md),包含 Go 特定內容。
|
|||
|
|
|
|||
|
|
## 格式化 (Formatting)
|
|||
|
|
|
|||
|
|
- **gofmt** 與 **goimports** 是強制性的 — 不存在風格爭論。
|
|||
|
|
|
|||
|
|
## 設計原則 (Design Principles)
|
|||
|
|
|
|||
|
|
- 接收介面 (Accept interfaces),返回結構體 (Return structs)。
|
|||
|
|
- 保持介面精簡 (1-3 個方法)。
|
|||
|
|
|
|||
|
|
## 錯誤處理 (Error Handling)
|
|||
|
|
|
|||
|
|
始終為錯誤包裹上下文:
|
|||
|
|
|
|||
|
|
```go
|
|||
|
|
if err != nil {
|
|||
|
|
return fmt.Errorf("failed to create user: %w", err)
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 參考資源
|
|||
|
|
|
|||
|
|
參見技能 (Skill):`golang-patterns`,獲取全面的 Go 慣用法與模式。
|