Standard library / go/ast

go/ast.IsGenerated()

Recognizes the standard generated-code marker in a file parsed with comments. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func IsGenerated(file *File) bool

Parameters & Attributes

NameTypeDescription
file*FileFile with a size matching the source, or file to remove from the set.

Return Value

bool

Recognizes the standard generated-code marker in a file parsed with comments.

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("// Code generated by demo. DO NOT EDIT.\npackage p")  fmt.Println(ast.IsGenerated(f))}
Go Reference