?
This document uses PHP Chinese website manual Release
import "go/doc"
Overview
Index
Package doc從Go AST中提取源代碼文檔。
Variables
func Examples(files ...*ast.File) []*Example
func IsPredeclared(s string) bool
func Synopsis(s string) string
func ToHTML(w io.Writer, text string, words map[string]string)
func ToText(w io.Writer, text string, indent, preIndent string, width int)
type Example
type Filter
type Func
type Mode
type Note
type Package
func New(pkg *ast.Package, importPath string, mode Mode) *Package
func (p *Package) Filter(f Filter)
type Type
type Value
comment.go doc.go example.go exports.go filter.go reader.go synopsis.go
var IllegalPrefixes = []string{ "copyright", "all rights", "author",}
func Examples(files ...*ast.File) []*Example
Examples返回按名稱字段排序的文件中找到的示例。Order字段記錄遇到示例的順序。
可播放的示例必須位于名稱以“_test”結(jié)尾的包中。在以下任一情況下,示例都是“可播放的”(播放字段不為零):
- The example function is self-contained: the function references only identifiers from other packages (or predeclared identifiers, such as "int") and the test file does not include a dot import.- The entire test file is the example: the file contains exactly one example function, zero test or benchmark functions, and at least one top-level function, type, variable, or constant declaration other than the example function.
func IsPredeclared(s string) bool
IsPredeclared報(bào)告s是否是預(yù)先標(biāo)識(shí)的標(biāo)識(shí)符。
func Synopsis(s string) string
Synopsis返回s中第一句話的清晰版本。該句在第一個(gè)句點(diǎn)后加空格,并且前面沒(méi)有一個(gè)大寫字母后結(jié)束。結(jié)果字符串沒(méi)有\(zhòng) n,\ r或\ t字符,并且在單詞之間只使用單個(gè)空格。如果s從任何非法前綴開(kāi)始,則結(jié)果是空字符串。
func ToHTML(w io.Writer, text string, words map[string]string)
ToHTML將注釋文本轉(zhuǎn)換為格式化的HTML。該評(píng)論是由DocReader編寫的,因此已知不會(huì)在行尾有空行,也不會(huì)在行尾有尾隨空格。評(píng)論標(biāo)記已被刪除。
不縮進(jìn)的非空行的每個(gè)跨度都被轉(zhuǎn)換為單個(gè)段落。規(guī)則有一個(gè)例外:由單一行組成的跨度,后面跟著另一段跨度,以大寫字母開(kāi)頭,并且不包含標(biāo)點(diǎn)符號(hào)被格式化為標(biāo)題。
縮進(jìn)行的跨度被轉(zhuǎn)換為<pre>塊,并刪除了公共縮進(jìn)前綴。
評(píng)論文本中的URL被轉(zhuǎn)換為鏈接; 如果URL也出現(xiàn)在單詞映射中,則鏈接將從映射中獲?。ㄈ绻鄳?yīng)的映射值是空字符串,則URL不會(huì)轉(zhuǎn)換為鏈接)。
出現(xiàn)在單詞映射中的Go標(biāo)識(shí)符以斜體表示; 如果相應(yīng)的映射值不是空字符串,則將其視為URL并將該單詞轉(zhuǎn)換為鏈接。
func ToText(w io.Writer, text string, indent, preIndent string, width int)
ToText以文本輸出的形式準(zhǔn)備評(píng)論文本。它將文本段落包裝成寬度或更少的Unicode代碼點(diǎn),然后將每行縮進(jìn)前綴。在預(yù)先格式化的部分(如程序文本)中,它使用preIndent前綴每個(gè)非空行。
一個(gè)Example 代表了一個(gè)在源文件中找到的示例函數(shù)。
type Example struct { Name string // name of the item being exemplified Doc string // example function doc string Code ast.Node Play *ast.File // a whole program version of the example Comments []*ast.CommentGroup Output string // expected output Unordered bool EmptyOutput bool // expect empty output Order int // original source code order}
type Filter func(string) bool
Func是func聲明的文檔。
type Func struct { Doc string Name string Decl *ast.FuncDecl // methods // (for functions, these fields have the respective zero value) Recv string // actual receiver "T" or "*T" Orig string // original receiver "T" or "*T" Level int // embedding level; 0 means not embedded}
Mode值控制New的操作。
type Mode int
const ( // extract documentation for all package-level declarations, // not just exported ones AllDecls Mode = 1 << iota // show all embedded methods, not just the ones of // invisible (unexported) anonymous fields AllMethods)
Note 代表以“MARKER(uid):note body”開(kāi)頭的標(biāo)注注釋。任何帶有2個(gè)或更多大寫AZ字母的標(biāo)記以及至少一個(gè)字符的uid都被識(shí)別。uid后面的“:”是可選的。注釋收集在由Note 標(biāo)記索引的Package.Notes地圖中。
type Note struct { Pos, End token.Pos // position range of the comment containing the marker UID string // uid found with the marker Body string // note body text}
Package 是整個(gè)包的文檔。
type Package struct { Doc string Name string ImportPath string Imports []string Filenames []string Notes map[string][]*Note // Deprecated: For backward compatibility Bugs is still populated, // but all new code should use Notes instead. Bugs []string // declarations Consts []*Value Types []*Type Vars []*Value Funcs []*Func}
func New(pkg *ast.Package, importPath string, mode Mode) *Package
New計(jì)算給定包AST的包文檔。New取得AST pkg的所有權(quán),并可以編輯或覆蓋它。
func (p *Package) Filter(f Filter)
過(guò)濾器消除了不通過(guò)過(guò)濾器f的名稱的文檔。TODO(gri):將“Type.Method”識(shí)別為名稱。
Type是類型聲明的文檔。
type Type struct { Doc string Name string Decl *ast.GenDecl // associated declarations Consts []*Value // sorted list of constants of (mostly) this type Vars []*Value // sorted list of variables of (mostly) this type Funcs []*Func // sorted list of functions returning this type Methods []*Func // sorted list of methods (including embedded ones) of this type}
Value 是(可能分組的)var或const聲明的文檔。
type Value struct { Doc string Names []string // var or const names in declaration order Decl *ast.GenDecl // contains filtered or unexported fields}