Golang go/ast.CommentMap.Comments() and Filter() functions example
package go/ast
go/ast.CommentMap.Comments()
Comments returns the list of comment groups in the comment map. The result is sorted is source order.
go/ast.CommentMap.Filter()
Filter returns a new comment map consisting of only those entries of cmap for which a corresponding node exists in the AST specified by node.
Golang go/ast.CommentMap.Comments() and Filter() functions example
Example( taken from http://golang.org/pkg/go/ast/#CommentMap )
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "src.go", src, parser.ParseComments)
if err != nil {
panic(err)
}
// Create an ast.CommentMap from the ast.File's comments.
// This helps keeping the association between comments
// and AST nodes.
cmap := ast.NewCommentMap(fset, f, f.Comments) // <-- here
go/ast.CommentMap.Filter() :
func packageExports(fset *token.FileSet, pkg *ast.Package) {
for _, src := range pkg.Files {
cmap := ast.NewCommentMap(fset, src, src.Comments)
ast.FileExports(src)
src.Comments = cmap.Filter(src).Comments()
}
}
References :
Advertisement
Something interesting
Tutorials
+6.6k Golang : Warp text string by number of characters or runes example
+24.1k Golang : Upload to S3 with official aws-sdk-go package
+43.3k Golang : Convert []byte to image
+7.7k Golang : Mapping Iban to Dunging alphabets
+5.8k Golang : Launching your executable inside a console under Linux
+11.2k Google Maps URL parameters configuration
+6k Javascript : Get operating system and browser information
+19.6k Golang : Set or Add HTTP Request Headers
+29.2k Golang : missing Git command
+11.5k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command