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
| Name | Type | Description |
|---|---|---|
| fset | *token.FileSet | File set used to parse source. |
| pkg | *Package | Package containing the symbol; nil for package-independent objects. |
| pos | token.Pos | Source position in the associated file set. |
| expr | ast.Expr | Expression source text or parsed expression. |
| info | *Info | Optional 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))}