Standard library / go/doc
go/doc.Example
Describes an extracted example, including code, expected output and source order. Expected stdout: 'true hello false\n'.
Formal Syntax & Structure
go syntaxgo
type Example struct { Name string Suffix string Doc string Code ast.Node Play *ast.File Comments []*ast.CommentGroup Output string Unordered bool EmptyOutput bool Order int}Working Example
Example Snippetgo
package main
import ( "fmt" "go/doc" "go/parser" "go/token" "strings")
func check(e error) { if e != nil { panic(e) }}
func main() { fs := token.NewFileSet() f, e := parser.ParseFile(fs, "demo_test.go", `package demo_testfunc Example(){// Output: hello}`, parser.ParseComments) check(e) xs := doc.Examples(f) var x *doc.Example = xs[0] fmt.Println(x.Name == "", strings.TrimSpace(x.Output), x.Unordered)}