package doc

Import Path
	go/doc (on and

Dependency Relation
	imports 13 packages, and imported by one package

Involved Source Files comment.go Package doc extracts source code documentation from a Go AST. example.go exports.go filter.go reader.go synopsis.go
Code Examples package main import ( "fmt" "go/ast" "go/doc" "go/parser" "go/token" ) func main() { // src and test are two source files that make up // a package whose documentation will be computed. const src = ` // This is the package comment. package p import "fmt" // This comment is associated with the Greet function. func Greet(who string) { fmt.Printf("Hello, %s!\n", who) } ` const test = ` package p_test // This comment is associated with the ExampleGreet_world example. func ExampleGreet_world() { Greet("world") } ` // Create the AST by parsing src and test. fset := token.NewFileSet() files := []*ast.File{ mustParse(fset, "src.go", src), mustParse(fset, "src_test.go", test), } // Compute package documentation with examples. p, err := doc.NewFromFiles(fset, files, "") if err != nil { panic(err) } fmt.Printf("package %s - %s", p.Name, p.Doc) fmt.Printf("func %s - %s", p.Funcs[0].Name, p.Funcs[0].Doc) fmt.Printf(" ⤷ example with suffix %q - %s", p.Funcs[0].Examples[0].Suffix, p.Funcs[0].Examples[0].Doc) } func mustParse(fset *token.FileSet, filename, src string) *ast.File { f, err := parser.ParseFile(fset, filename, src, parser.ParseComments) if err != nil { panic(err) } return f }
Package-Level Type Names (total 8)
/* sort by: | */
An Example represents an example function found in a test source file. Code ast.Node Comments []*ast.CommentGroup // example function doc string // expect empty output // name of the item being exemplified (including optional suffix) // original source code order // expected output // a whole program version of the example // example suffix, without leading '_' (only populated by NewFromFiles) Unordered bool func Examples(testFiles ...*ast.File) []*Example
func (*Package).Filter(f Filter)
Func is the documentation for a func declaration. Decl *ast.FuncDecl Doc string Examples is a sorted list of examples associated with this function or method. Examples are extracted from _test.go files provided to NewFromFiles. // embedding level; 0 means not embedded Name string // original receiver "T" or "*T" methods (for functions, these fields have the respective zero value) // actual receiver "T" or "*T"
Mode values control the operation of New and NewFromFiles. func New(pkg *ast.Package, importPath string, mode Mode) *Package const AllDecls const AllMethods const PreserveAST
A Note represents a marked comment starting with "MARKER(uid): note body". Any note with a marker of 2 or more upper case [A-Z] letters and a uid of at least one character is recognized. The ":" following the uid is optional. Notes are collected in the Package.Notes map indexed by the notes marker. // note body text // position range of the comment containing the marker // position range of the comment containing the marker // uid found with the marker
Package is the documentation for an entire package. Deprecated: For backward compatibility Bugs is still populated, but all new code should use Notes instead. declarations Doc string Examples is a sorted list of examples associated with the package. Examples are extracted from _test.go files provided to NewFromFiles. Filenames []string Funcs []*Func ImportPath string Imports []string Name string Notes map[string][]*Note Types []*Type Vars []*Value Filter eliminates documentation for names that don't pass through the filter f. TODO(gri): Recognize "Type.Method" as a name. func New(pkg *ast.Package, importPath string, mode Mode) *Package func NewFromFiles(fset *token.FileSet, files []*ast.File, importPath string, opts ...interface{}) (*Package, error)
Type is the documentation for a type declaration. associated declarations // sorted list of constants of (mostly) this type Decl *ast.GenDecl Doc string Examples is a sorted list of examples associated with this type. Examples are extracted from _test.go files provided to NewFromFiles. // sorted list of functions returning this type // sorted list of methods (including embedded ones) of this type Name string // sorted list of variables of (mostly) this type
Value is the documentation for a (possibly grouped) var or const declaration. Decl *ast.GenDecl Doc string // var or const names in declaration order
Package-Level Functions (total 7)
Examples returns the examples found in testFiles, sorted by Name field. The Order fields record the order in which the examples were encountered. The Suffix field is not populated when Examples is called directly, it is only populated by NewFromFiles for examples it finds in _test.go files. Playable Examples must be in a package whose name ends in "_test". An Example is "playable" (the Play field is non-nil) in either of these circumstances: - 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.
IsPredeclared reports whether s is a predeclared identifier.
New computes the package documentation for the given package AST. New takes ownership of the AST pkg and may edit or overwrite it. To have the Examples fields populated, use NewFromFiles and include the package's _test.go files.
NewFromFiles computes documentation for a package. The package is specified by a list of *ast.Files and corresponding file set, which must not be nil. NewFromFiles uses all provided files when computing documentation, so it is the caller's responsibility to provide only the files that match the desired build context. "go/build".Context.MatchFile can be used for determining whether a file matches a build context with the desired GOOS and GOARCH values, and other build constraints. The import path of the package is specified by importPath. Examples found in _test.go files are associated with the corresponding type, function, method, or the package, based on their name. If the example has a suffix in its name, it is set in the Example.Suffix field. Examples with malformed names are skipped. Optionally, a single extra argument of type Mode can be provided to control low-level aspects of the documentation extraction behavior. NewFromFiles takes ownership of the AST files and may edit them, unless the PreserveAST Mode bit is on.
Synopsis returns a cleaned version of the first sentence in s. That sentence ends after the first period followed by space and not preceded by exactly one uppercase letter. The result string has no \n, \r, or \t characters and uses only single spaces between words. If s starts with any of the IllegalPrefixes, the result is the empty string.
ToHTML converts comment text to formatted HTML. The comment was prepared by DocReader, so it is known not to have leading, trailing blank lines nor to have trailing spaces at the end of lines. The comment markers have already been removed. Each span of unindented non-blank lines is converted into a single paragraph. There is one exception to the rule: a span that consists of a single line, is followed by another paragraph span, begins with a capital letter, and contains no punctuation other than parentheses and commas is formatted as a heading. A span of indented lines is converted into a <pre> block, with the common indent prefix removed. URLs in the comment text are converted into links; if the URL also appears in the words map, the link is taken from the map (if the corresponding map value is the empty string, the URL is not converted into a link). A pair of (consecutive) backticks (`) is converted to a unicode left quote (“), and a pair of (consecutive) single quotes (') is converted to a unicode right quote (”). Go identifiers that appear in the words map are italicized; if the corresponding map value is not the empty string, it is considered a URL and the word is converted into a link.
ToText prepares comment text for presentation in textual output. It wraps paragraphs of text to width or fewer Unicode code points and then prefixes each line with the indent. In preformatted sections (such as program text), it prefixes each non-blank line with preIndent. A pair of (consecutive) backticks (`) is converted to a unicode left quote (“), and a pair of (consecutive) single quotes (') is converted to a unicode right quote (”).
Package-Level Variables (only one)
Package-Level Constants (total 3)
AllDecls says to extract documentation for all package-level declarations, not just exported ones.
AllMethods says to show all embedded methods, not just the ones of invisible (unexported) anonymous fields.
PreserveAST says to leave the AST unmodified. Originally, pieces of the AST such as function bodies were nil-ed out to save memory in godoc, but not all programs want that behavior.