Standard library / go/doc

go/doc.Func

Describes a function or method declaration. Expected stdout: 'Work true\n'.

Formal Syntax & Structure

go syntaxgo
type Func struct {  Doc  string  Name string  Decl *ast.FuncDecl
  Recv  string  Orig  string  Level int
  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 f *doc.Func = p.Funcs[0]; fmt.Println(f.Name, f.Recv == "") }
Go Reference