Golang go/ast.Field type, End() and Pos() functions examples

package go/ast

A Field represents a Field declaration list in a struct type, a method list in an interface type, or a parameter/result declaration in a signature.

Golang go/ast.Field type, End() 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.Field:
 // Can be any of these:
 // struct {x, y int}  -- struct field(s)
 // struct {T} -- anon struct field
 // interface {I} -- interface embedding
 // interface {f()} -- interface method
 // func (A) func(B) C -- receiver, param(s), result(s)
 return "field/method/parameter"
 ...
 }

Example 2:

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

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

Example 3:

 func checkFieldTag(f *File, node ast.Node) {
 field := node.(*ast.Field)
 if field.Tag == nil {
 return
 }
 ...
 }

References :

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

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

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

Advertisement