?
本文檔使用 PHP中文網(wǎng)手冊 發(fā)布
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報告s是否是預先標識的標識符。
func Synopsis(s string) string
Synopsis返回s中第一句話的清晰版本。該句在第一個句點后加空格,并且前面沒有一個大寫字母后結(jié)束。結(jié)果字符串沒有\(zhòng) n,\ r或\ t字符,并且在單詞之間只使用單個空格。如果s從任何非法前綴開始,則結(jié)果是空字符串。
func ToHTML(w io.Writer, text string, words map[string]string)
ToHTML將注釋文本轉(zhuǎn)換為格式化的HTML。該評論是由DocReader編寫的,因此已知不會在行尾有空行,也不會在行尾有尾隨空格。評論標記已被刪除。
不縮進的非空行的每個跨度都被轉(zhuǎn)換為單個段落。規(guī)則有一個例外:由單一行組成的跨度,后面跟著另一段跨度,以大寫字母開頭,并且不包含標點符號被格式化為標題。
縮進行的跨度被轉(zhuǎn)換為<pre>塊,并刪除了公共縮進前綴。
評論文本中的URL被轉(zhuǎn)換為鏈接; 如果URL也出現(xiàn)在單詞映射中,則鏈接將從映射中獲?。ㄈ绻鄳?yīng)的映射值是空字符串,則URL不會轉(zhuǎn)換為鏈接)。
出現(xiàn)在單詞映射中的Go標識符以斜體表示; 如果相應(yīng)的映射值不是空字符串,則將其視為URL并將該單詞轉(zhuǎn)換為鏈接。
func ToText(w io.Writer, text string, indent, preIndent string, width int)
ToText以文本輸出的形式準備評論文本。它將文本段落包裝成寬度或更少的Unicode代碼點,然后將每行縮進前綴。在預先格式化的部分(如程序文本)中,它使用preIndent前綴每個非空行。
一個Example 代表了一個在源文件中找到的示例函數(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”開頭的標注注釋。任何帶有2個或更多大寫AZ字母的標記以及至少一個字符的uid都被識別。uid后面的“:”是可選的。注釋收集在由Note 標記索引的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 是整個包的文檔。
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計算給定包AST的包文檔。New取得AST pkg的所有權(quán),并可以編輯或覆蓋它。
func (p *Package) Filter(f Filter)
過濾器消除了不通過過濾器f的名稱的文檔。TODO(gri):將“Type.Method”識別為名稱。
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}