Standard library / go/types

go/types.NewChecker()

Creates a stateful checker for a package; add parsed files with Files. Expected stdout: 'int\n'.

Formal Syntax & Structure

go syntaxgo
func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Checker

Parameters & Attributes

NameTypeDescription
conf*ConfigType checker configuration.
fset*token.FileSetFile set used to parse source.
pkg*PackagePackage containing the symbol; nil for package-independent objects.
info*InfoOptional destination for semantic information; initialize desired maps.

Return Value

*Checker

Creates a stateful checker for a package; add parsed files with Files.

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]).Type())}
Go Reference