Source File
example.go
Belonging Package
go/doc
package doc
import (
)
type Example struct {
Name string // name of the item being exemplified (including optional suffix)
Suffix string // example suffix, without leading '_' (only populated by NewFromFiles)
Doc string // example function doc string
Code ast.Node
Play *ast.File // a whole program version of the example
Comments []*ast.CommentGroup
Output string // expected output
Unordered bool
EmptyOutput bool // expect empty output
Order int // original source code order
}
func ( ...*ast.File) []*Example {
var []*Example
for , := range {
:= false // file contains tests or benchmarks
:= 0 // number of non-import declarations in the file
var []*Example
for , := range .Decls {
if , := .(*ast.GenDecl); && .Tok != token.IMPORT {
++
continue
}
, := .(*ast.FuncDecl)
if ! || .Recv != nil {
continue
}
++
:= .Name.Name
if isTest(, "Test") || isTest(, "Benchmark") {
= true
continue
}
if !isTest(, "Example") {
continue
}
if := .Type.Params; len(.List) != 0 {
continue // function has params; not a valid example
}
if .Body == nil { // ast.File.Body nil dereference (see issue 28044)
continue
}
var string
if .Doc != nil {
= .Doc.Text()
}
, , := exampleOutput(.Body, .Comments)
= append(, &Example{
Name: [len("Example"):],
Doc: ,
Code: .Body,
Play: playExample(, ),
Comments: .Comments,
Output: ,
Unordered: ,
EmptyOutput: == "" && ,
Order: len(),
})
}
[0].Code =
[0].Play = playExampleFile()
}
= append(, ...)
sort.Slice(, func(, int) bool {
return [].Name < [].Name
})
return
}
var outputPrefix = lazyregexp.New(`(?i)^[[:space:]]*(unordered )?output:`)
:= .Text()
if := outputPrefix.FindStringSubmatchIndex(); != nil {
if [2] != -1 {
= true
}
return nil
}
:= make(map[*ast.Object]ast.Decl)
:= make(map[string][]ast.Decl)
for , := range .Decls {
switch d := .(type) {
case *ast.FuncDecl:
if .Recv == nil {
[.Name.Obj] =
} else {
if len(.Recv.List) == 1 {
:= .Recv.List[0].Type
, := baseTypeName()
[] = append([], )
}
}
case *ast.GenDecl:
for , := range .Specs {
switch s := .(type) {
case *ast.TypeSpec:
[.Name.Obj] =
case *ast.ValueSpec:
for , := range .Names {
[.Obj] =
}
}
}
}
}
if .Type.Params != nil {
for , := range .Type.Params.List {
ast.Inspect(.Type, )
}
}
if .Type.Results != nil {
for , := range .Type.Results.List {
ast.Inspect(.Type, )
}
}
ast.Inspect(.Body, )
case *ast.GenDecl:
for , := range .Specs {
switch s := .(type) {
case *ast.TypeSpec:
ast.Inspect(.Type, )
= append(, [.Name.Name]...)
case *ast.ValueSpec:
if .Type != nil {
ast.Inspect(.Type, )
}
for , := range .Values {
ast.Inspect(, )
}
}
}
}
}
for := range {
if predeclaredTypes[] || predeclaredConstants[] || predeclaredFuncs[] {
delete(, )
}
}
var []*ast.CommentGroup
for , := range {
if := .(*ast.ImportSpec).Doc; != nil {
= append(, )
}
}
, = stripOutputComment(, )
:= &ast.GenDecl{
Tok: token.IMPORT,
Lparen: 1, // Need non-zero Lparen and Rparen so that printer
Rparen: 1, // treats this as a factored import.
}
for , := range {
:= &ast.ImportSpec{Path: &ast.BasicLit{Value: strconv.Quote()}}
if path.Base() != {
.Name = ast.NewIdent()
}
.Specs = append(.Specs, )
}
.Specs = append(.Specs, ...)
, := lastComment(, )
if == nil || !outputPrefix.MatchString(.Text()) {
return ,
}
:= make(map[string]*[]*Example)
[""] = &.Examples // package-level examples have an empty name
for , := range .Funcs {
if !token.IsExported(.Name) {
continue
}
[.Name] = &.Examples
}
for , := range .Types {
if !token.IsExported(.Name) {
continue
}
[.Name] = &.Examples
for , := range .Funcs {
if !token.IsExported(.Name) {
continue
}
[.Name] = &.Examples
}
for , := range .Methods {
if !token.IsExported(.Name) {
continue
}
[strings.TrimPrefix(.Recv, "*")+"_"+.Name] = &.Examples
}
}
for := len(.Name); >= 0; = strings.LastIndexByte(.Name[:], '_') {
, , := splitExampleName(.Name, )
if ! {
continue
}
, := []
if ! {
continue
}
.Suffix =
* = append(*, )
break
}
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |