Golang go/ast.FieldList type, End(), NumFields() and Pos() functions example

package go/ast

A FieldList represents a list of Fields, enclosed by parentheses or braces.

NumFields returns the number of (named and anonymous fields) in a FieldList.

Golang go/ast.FieldList type, End(), NumFields() and Pos() functions usage examples

Example 1:

 func NodeDescription(n ast.Node) string {
 switch n := n.(type) {
 case *ast.ArrayType:
 return "array type"
 case *ast.FieldList:
 return "field/method/parameter list"
 ...
 }

Example 2:

 func checkCurrentPosition(node ast.Node) string {
 n := node.(*ast.FieldList)
 endpos := n.End()
 pospos := n.Pos()

 if pospos != endpos {
 return "not yet"
 }
 }

Example 3:

 var sig *ast.FuncType
 res := sig.Results
 n := res.NumFields()
 if n == 0 {
 // do something
 }

Reference :

http://golang.org/pkg/go/ast/#FieldList

http://golang.org/pkg/go/ast/#FieldList.NumFields

http://golang.org/pkg/go/ast/#FieldList.End

http://golang.org/pkg/go/ast/#FieldList.Pos

Advertisement