Golang go/ast.CommentGroup.Text() function examples

package go/ast

Text returns the text of the comment. Comment markers (//, /*, and */), the first space of a line comment, and leading and trailing empty lines are removed. Multiple empty lines are reduced to one, and trailing space on lines is trimmed. Unless the result is empty, it is newline-terminated.

Golang go/ast.CommentGroup.Text() function usage example

Example 1:

 func saveConfig(pkg *build.Package, filename string, cg *ast.CommentGroup) error {
 text := cg.Text() // <-- here
 for _, line := range strings.Split(text, "\n") {
 orig := line 
 ....
 }

Example 2:

 func importComment(s ast.Spec) string {
  c := s.(*ast.ImportSpec).Comment
  if c == nil {
 return ""
  }
  return c.Text()
 }

Reference :

http://golang.org/pkg/go/ast/#CommentGroup.Text

Advertisement