Standard library / go/ast

go/ast.CommentGroup.Text()

Returns normalized comment text without comment markers, directives or surrounding blank lines. Expected stdout: '"hello\\n"\n'.

Formal Syntax & Structure

go syntaxgo
func (g *CommentGroup) Text() string

Return Value

string

Returns normalized comment text without comment markers, directives or surrounding blank lines.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/ast"  "go/parser"  "go/token")
func check(e error) {  if e != nil {    panic(e)  }}func parsed(src string) (*token.FileSet, *ast.File) {  fs := token.NewFileSet()  f, e := parser.ParseFile(fs, "demo.go", src, parser.ParseComments|parser.SkipObjectResolution)  check(e)  return fs, f}func find[T ast.Node](root ast.Node) T {  for n := range ast.Preorder(root) {    if x, ok := n.(T); ok {      return x    }  }  panic("node not found")}
func main() {  _, f := parsed("package p\n// hello\nvar x int")  fmt.Printf("%q\n", f.Comments[0].Text())}
Go Reference