Standard library / go/ast

go/ast.CommentGroup.End()

Returns the compact position immediately after this CommentGroup node. Positions refer to the original source, not a freshly formatted representation. Expected stdout: '28\n'.

Formal Syntax & Structure

go syntaxgo
func (g *CommentGroup) End() token.Pos

Return Value

token.Pos

Returns the compact position immediately after this CommentGroup node.

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() {  src := "package p\n// first\n// second\nvar x int"  fs, f := parsed(src)  n := find[*ast.CommentGroup](f)  fmt.Println(fs.Position(n.End()).Offset)}
Go Reference