Standard library / go/doc
go/doc.Examples()
Extracts example functions from test ASTs, including declared expected output; does not execute them. Expected stdout: '1 hello\n'.
Formal Syntax & Structure
go syntaxgo
func Examples(testFiles ...*ast.File) []*ExampleParameters & Attributes
| Name | Type | Description |
|---|---|---|
| testFiles | ...*ast.File | Input to the operation as described above. |
Return Value
[]*Example
Extracts example functions from test ASTs, including declared expected output; does not execute them.
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) fmt.Println(len(xs), strings.TrimSpace(xs[0].Output))}