Standard library / go/types
go/types.Config.Check()
Checks parsed files as a package, reporting semantic errors and filling requested Info maps. Expected stdout: 'int\n'.
Formal Syntax & Structure
go syntaxgo
func (conf *Config) Check(path string, fset *token.FileSet, files []*ast.File, info *Info) (*Package, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| path | string | Package import path. |
| fset | *token.FileSet | File set used to parse source. |
| files | []*ast.File | Parsed source files belonging to the same package. |
| info | *Info | Optional destination for semantic information; initialize desired maps. |
Return Value
(*Package, error)
Checks parsed files as a package, reporting semantic errors and filling requested Info maps.
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 load() (*token.FileSet, *ast.File, *types.Package, *types.Info) { f := token.NewFileSet() a, e := parser.ParseFile(f, "demo.go", "package p; var X = 2+3; func F(){ x := X; _ = x }", parser.SkipObjectResolution) check(e) i := &types.Info{Types: map[ast.Expr]types.TypeAndValue{}, Defs: map[*ast.Ident]types.Object{}, Uses: map[*ast.Ident]types.Object{}, Scopes: map[ast.Node]*types.Scope{}} c := types.Config{GoVersion: "go1.23"} p, e := c.Check("demo/p", f, []*ast.File{a}, i) check(e) return f, a, p, i}func main() { _, _, p, _ := load(); fmt.Println(p.Scope().Lookup("X").Type()) }