Standard library / go/doc

go/doc.New()

Extracts documentation from an ast.Package; may modify the AST. Prefer NewFromFiles when extracting examples. Expected stdout: 'demo X\n'.

Formal Syntax & Structure

go syntaxgo
func New(pkg *ast.Package, importPath string, mode Mode) *Package

Parameters & Attributes

NameTypeDescription
pkg*ast.PackagePackage containing the symbol; nil for package-independent objects.
importPathstringInput to the operation as described above.
modeModeInput to the operation as described above.

Return Value

*Package

Extracts documentation from an ast.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/ast"  "go/doc"  "go/parser"  "go/token")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  fs := token.NewFileSet()  f, e := parser.ParseFile(fs, "demo.go", "package demo; const X=1", 0)  check(e)  p := doc.New(&ast.Package{Name: "demo", Files: map[string]*ast.File{"demo.go": f}}, "example.invalid/demo", 0)  fmt.Println(p.Name, p.Consts[0].Names[0])}
Go Reference