Standard library / go/doc
go/doc.Type
Describes a documented type and its associated declarations. Expected stdout: 'Item\n'.
Formal Syntax & Structure
go syntaxgo
type Type struct { Doc string Name string Decl *ast.GenDecl
Consts []*Value Vars []*Value Funcs []*Func Methods []*Func
Examples []*Example}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 packageDoc(mode doc.Mode) *doc.Package { fs := token.NewFileSet() f, e := parser.ParseFile(fs, "demo.go", `// Package demo documents values.package demo// Answer is a number.const Answer=42// hidden is private.var hidden=1// Item describes a value.type Item struct{}// Work performs work.func Work(){}// BUG(user): fix example.`, parser.ParseComments) check(e) p, e := doc.NewFromFiles(fs, []*ast.File{f}, "example.invalid/demo", mode) check(e) return p}
func main() { p := packageDoc(0); var x *doc.Type = p.Types[0]; fmt.Println(x.Name) }