🐹 Go Quick Reference

📐 工作原理與說明
按分類聚合條目:遍歷條目列表,首次出現的分類記入有序陣列並把計數置 1,後續同分類累加;柱狀圖以分類名為橫軸、條目數為資料(圖高 300),明細表逐行輸出序號、分類名、該分類條目數與該分類前 2 條代表命令(以「 / 」連線)。分類名在英文態下按內建對映表替換為英文(Go 語法與標準庫)
Go 語言常用語法與標準庫速查。
  • 核心:變數與常量、結構體、方法、介面、defer、panic/recover。
  • 併發:go 關鍵字啟動 goroutine,channel 通訊,select 多路複用。
  • 錯誤:顯式返回 error,用 errors.Is/errors.As 判定;Go 沒有異常機制。
  • 常用包:fmt、strings、strconv、time、encoding/json、net/http、sync。

Browse a Go cheat sheet in your browser — syntax, concurrency and standard-library snippets.

📚 深度解析:Go 速查

💡 常見使用場景

  • 查閱 goroutine/defer/err 處理。
  • 寫併發程式碼時查 channel 與 select 的慣用法,確認關閉 channel 與 range 的配合,避免寫出會永久阻塞的 goroutine。
查錯誤
`if err != nil { return err }` 慣用法。

❓ 常見問題(FAQ)

defer 何時執行?
函式返回前按後進先出執行。
goroutine 洩漏怎麼避免?
核心是保證每個 goroutine 都有退出路徑:用 context 傳遞取消訊號、給 channel 操作加 select + ctx.Done() 分支、並確保傳送方最終會關閉 channel。常見於等待一個永遠沒人寫入的 channel——可用 pprof 觀察 goroutine 數量是否持續增長。

Formula & Standard References

Item Formula / Note Source / Standard
Specification syntax and semantics defined by the Go Language Specification Go Language Specification
Concurrency goroutines with channels for communication Go Language Specification
Error handling errors are interface values returned and checked explicitly Go Language Specification
⚠️ 使用說明與注意事項