Standard library / go/types

go/types.Info

Stores semantic maps requested by the caller; nil maps are not populated. Expected stdout: 'true true\n'.

Formal Syntax & Structure

go syntaxgo
type Info struct {  Types map[ast.Expr]TypeAndValue
  Instances map[*ast.Ident]Instance
  Defs map[*ast.Ident]Object
  Uses map[*ast.Ident]Object
  Implicits map[ast.Node]Object
  Selections map[*ast.SelectorExpr]*Selection
  Scopes map[ast.Node]*Scope
  InitOrder []*Initializer
  FileVersions map[*ast.File]string}

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() { _, _, _, i := load(); fmt.Println(len(i.Defs) > 0, len(i.Uses) > 0) }
Go Reference