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
+7k Web : How to see your website from different countries?
+8.6k Golang : Progress bar with ∎ character
+6.8k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+10.8k Android Studio : Checkbox for user to select options example
+8.3k Golang : Configure Apache and NGINX to access your Go service example
+12.7k Golang : zlib compress file example
+3.4k Golang : Fix go-cron set time not working issue
+5.6k Swift : Get substring with rangeOfString() function example
+7.8k Golang : Scan files for certain pattern and rename part of the files
+15.9k Golang : Read a file line by line
+8.2k Golang : Add build version and other information in executables
+14.2k Golang : Fix image: unknown format error