Go Wiki:註解
每個套件都應該有一個套件註解。它應該緊接在套件中某個檔案的 package
陳述句之前。(它只需要出現在一個檔案中。)它應該以單一句子開頭,句子開頭為「套件 套件名稱」,並簡潔摘要套件功能。這個引言句子將會用在 godoc 的所有套件清單中。
後續句子和/或段落可以提供更多細節。句子應該適當標點符號。
// Package superman implements methods for saving the world.
//
// Experience has shown that a small number of procedures can prove
// helpful when attempting to save the world.
package superman
幾乎每個頂層類型、常數、變數和函式都應該有一個註解。bar 的註解應該採用「bar 飄浮在山谷和山丘的高空上。」的形式。bar 的第一個字母不應該大寫,除非它在程式碼中是大寫的。
// enterOrbit causes Superman to fly into low Earth orbit, a position
// that presents several possibilities for planet salvation.
func enterOrbit() os.Error {
...
}
你在註解中縮排的所有文字,godoc 都會將其呈現為預先格式化的區塊。這有助於程式碼範例。
// fight can be used on any enemy and returns whether Superman won.
//
// Examples:
//
// fight("a random potato")
// fight(LexLuthor{})
//
func fight(enemy interface{}) bool {
...
}
此內容是 Go Wiki 的一部分。