Coding
PromptBeginner5 minmarkdown
Nano Banana Pro
Agent skill for nano-banana-pro
7
generic skill
Sign in to like and favorite skills
具體展示關鍵規則如何落地。
// Package client 提供與遠端服務互動的 HTTP 用戶端。 // 零值不可用,請使用 New 建構子建立。 package client import ( "context" // 佈線取消與逾時的標準機制 "encoding/json" // 編解碼輸入/輸出 "errors" // 錯誤比對 "fmt" // 錯誤包裝與格式化 "io" // I/O 介面 "net/http" // HTTP 基礎 "time" // 逾時與回退間隔 ) // ErrNotFound:對應遠端 404 的語意錯誤(sentinel error)。 var ErrNotFound = errors.New("resource not found") // 小寫開頭,不加標點 // Client 僅保存不可變設定與共享 *http.Client;不保存請求狀態。 type Client struct { baseURL string // 基底位址(不可含尾斜線) httpClient *http.Client // 可注入以便測試與重用 Transport } // New 建立可用的 Client;呼叫者可注入自定 *http.Client。 func New(baseURL string, hc *http.Client) *Client { if hc == nil { hc = &http.Client{Timeout: 15 * time.Second} // 安全預設 } return &Client{baseURL: baseURL, httpClient: hc} } // Resource 對外輸出時含有 json 標籤,零值可用。 type Resource struct { ID string `json:"id"` Name string `json:"name,omitempty"` UpdatedAt time.Time `json:"updatedAt"` // RFC3339 UTC } // Get 透過 context 控制逾時/取消,正確關閉 Body 並轉換語意錯誤。 func (c *Client) Get(ctx context.Context, id string) (Resource, error) { var out Resource req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/v1/resources/"+id, nil) if err != nil { return out, fmt.Errorf("new request: %w", err) } resp, err := c.httpClient.Do(req) if err != nil { return out, fmt.Errorf("do request: %w", err) } defer resp.Body.Close() // 確保釋放連線 switch resp.StatusCode { case http.StatusOK: dec := json.NewDecoder(resp.Body) dec.DisallowUnknownFields() if err := dec.Decode(&out); err != nil { return out, fmt.Errorf("decode: %w", err) } return out, nil case http.StatusNotFound: // 將 HTTP 狀態轉換為語意錯誤 io.Copy(io.Discard, resp.Body) // 盡量讀完以便連線重用 return out, ErrNotFound default: b, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10)) // 限流保護 return out, fmt.Errorf("unexpected status %d: %s", resp.StatusCode, string(b)) } }
net/http - 標準庫,適合簡單 APIgithub.com/gin-gonic/gin - 高效能 Web 框架database/sql - 標準 SQL 介面github.com/jmoiron/sqlx - SQL 擴充gorm.io/gorm - ORM 框架github.com/spf13/cobra - CLI 框架github.com/spf13/viper - 設定管理github.com/vincent119/zlogger - 高效能日誌github.com/go-playground/validator - Struct 驗證go.uber.org/fx - 依賴注入 (DI)github.com/prometheus/client_golang - Prometheus Metricsgithub.com/redis/go-redis/v9 - Redis Clientgithub.com/vincent119/commons - 常用工具庫github.com/swaggo/swag - Swagger 文件產生uber-go/mock - Mock 生成工具 (gomock)shields.io - 狀態徽章 (README 用)package 宣告(置頂)gofmt -s / goimports / go veterr 立即檢查並以 %w 包裝;跨層以 errors.Is/Asgo.mod 依賴釘版;go mod tidy 後無不明變更New 實體context (Trace ID).gitignore(建議)# Go *.exe *.test *.out coverage.out vendor/ # IDE .idea/ .vscode/ *.swp # 環境與敏感資訊 .env *.local.yaml
Makefile(節選).PHONY: tidy lint test bench cover fmt vet swagger tidy: go mod tidy lint: golangci-lint run ./... test: go test -race -count=1 ./... bench: go test -run=NONE -bench=. -benchmem ./... # 執行測試並顯示覆蓋率 cover: go test -cover ./... # 產生覆蓋率報告(HTML) cover-html: go test -coverprofile=coverage.out ./... go tool cover -html=coverage.out # 格式化程式碼 fmt: go fmt ./... # 靜態分析 vet: go vet ./... # 更新 Swagger 文檔 swagger: swag init -g cmd/main.go
# 執行特定測試 go test -run TestFunctionName ./path/to/package # 執行特定 package 的所有測試 go test -v ./internal/order/... # 執行測試並產生覆蓋率報告 go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out
.golangci.yml(節選)run: timeout: 5m linters: enable: - errcheck - gocritic - gofumpt - govet - ineffassign - staticcheck - unparam - prealloc - revive - gosec linters-settings: gosec: excludes: - G404 revive: ignore-generated-header: true issues: exclude-use-default: false
建議存放路徑:
.github/instructions/go.DDD.instructions.md
此設定將自動套用於所有 Go 檔案(*.go, go.mod, go.sum)。