Golang go/ast.FilterFile() function example

package go/ast

FilterFile trims the AST for a Go file in place by removing all names from top-level declarations (including struct field and interface method names, but not from parameter lists) that don't pass through the filter f. If the declaration is empty afterwards, the declaration is removed from the AST. The File.Comments list is not changed.

FilterFile returns true if there are any top-level declarations left after filtering; it returns false otherwise.

Golang go/ast.FilterFile() function usage example

 var PAst map[string]*ast.File

 filter := "$" // Regular Expression based filter will be useful 

 for name, a := range PAst {
 ast.FilterFile(a, filter)
 …
 }

References :

https://github.com/fzipp/pythia/blob/master/third_party/go.tools/godoc/cmdline.go

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

Advertisement