Standard library / go/types
go/types.Checker.Files()
Checks parsed files as part of the checker package. Expected stdout: 'X\n'.
Formal Syntax & Structure
go syntaxgo
func (check *Checker) Files(files []*ast.File) (err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| files | []*ast.File | Parsed source files belonging to the same package. |
Return Value
(err error)
Checks parsed files as part of the checker package.
Working Example
Example Snippetgo
package main
import ( "fmt" "go/ast" "go/parser" "go/token" "go/types")
func check(e error) { if e != nil { panic(e) }}func checker() (*types.Checker, *ast.File) { f := token.NewFileSet() a, e := parser.ParseFile(f, "p.go", "package p; var X = 42", 0) check(e) c := types.NewChecker(&types.Config{}, f, types.NewPackage("p", "p"), &types.Info{Defs: map[*ast.Ident]types.Object{}, Types: map[ast.Expr]types.TypeAndValue{}}) return c, a}
func main() { c, f := checker() check(c.Files([]*ast.File{f})) fmt.Println(c.ObjectOf(f.Decls[0].(*ast.GenDecl).Specs[0].(*ast.ValueSpec).Names[0]).Name())}