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
+12.3k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+47.8k Golang : Convert int to byte array([]byte)
+4.6k Linux : sudo yum updates not working
+16.4k Golang : How to implement two-factor authentication?
+6.7k Golang : Experimental emojis or emoticons icons programming language
+16.6k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+26.1k Mac/Linux and Golang : Fix bind: address already in use error
+11.6k Android Studio : Create custom icons for your application example
+22.7k Golang : Strings to lowercase and uppercase example
+25.8k Golang : Daemonizing a simple web server process example
+37.7k Golang : Comparing date or timestamp
+7.9k Javascript : Put image into Chrome browser's console