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
+5.4k Golang : fmt.Println prints out empty data from struct
+6.5k PHP : Shuffle to display different content or advertisement
+15.3k Golang : How to get Unix file descriptor for console and file
+13.9k Golang : convert(cast) string to float value
+11.9k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+7.2k Golang : Check if one string(rune) is permutation of another string(rune)
+6.1k Golang : Measure execution time for a function
+9k Golang : automatically figure out array length(size) with three dots
+17.7k How to enable MariaDB/MySQL logs ?
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+6.8k Golang : Get expvar(export variables) to work with multiplexer
+31.1k Golang : Calculate percentage change of two values