Standard library / go/ast
go/ast.FieldList.NumFields()
Counts individual named fields and unnamed entries, not just Field nodes. A nil receiver yields zero. Expected stdout: '1 2\n'.
Formal Syntax & Structure
go syntaxgo
func (f *FieldList) NumFields() intReturn Value
int
Counts individual named fields and unnamed entries, not just Field nodes.
Working Example
Example Snippetgo
package main
import ( "fmt" "go/ast" "go/parser" "go/token")
func check(e error) { if e != nil { panic(e) }}func parsed(src string) (*token.FileSet, *ast.File) { fs := token.NewFileSet() f, e := parser.ParseFile(fs, "demo.go", src, parser.ParseComments|parser.SkipObjectResolution) check(e) return fs, f}func find[T ast.Node](root ast.Node) T { for n := range ast.Preorder(root) { if x, ok := n.(T); ok { return x } } panic("node not found")}
func main() { _, f := parsed("package p; func f(a,b int){}") p := find[*ast.FuncDecl](f).Type.Params fmt.Println(len(p.List), p.NumFields())}