Golang go/ast.CommentMap type and NewCommentMap() function example

package go/ast

A CommentMap maps an AST node to a list of comment groups associated with it. See NewCommentMap for a description of the association.

Golang go/ast.CommentMap type and NewCommentMap() function usage 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)

References :

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

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

Advertisement