Standard library / go/types

go/types.CheckExpr()

Type-checks a parsed expression and populates the requested Info maps. Expected stdout: 'untyped int\n'.

Formal Syntax & Structure

go syntaxgo
func CheckExpr(fset *token.FileSet, pkg *Package, pos token.Pos, expr ast.Expr, info *Info) (err error)

Parameters & Attributes

NameTypeDescription
fset*token.FileSetFile set used to parse source.
pkg*PackagePackage containing the symbol; nil for package-independent objects.
postoken.PosSource position in the associated file set.
exprast.ExprExpression source text or parsed expression.
info*InfoOptional destination for semantic information; initialize desired maps.

Return Value

(err error)

Type-checks a parsed expression and populates the 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 main() {  e, err := parser.ParseExpr("2+3")  check(err)  i := types.Info{Types: map[ast.Expr]types.TypeAndValue{}}  check(types.CheckExpr(token.NewFileSet(), nil, token.NoPos, e, &i))  fmt.Println(i.TypeOf(e))}
Go Reference