Standard library / go/parser

go/parser.ParseExprFrom()

Parses expression input and records positions in a non-nil file set. Expected stdout: 'expr.go:1:1\n'.

Formal Syntax & Structure

go syntaxgo
func ParseExprFrom(fset *token.FileSet, filename string, src any, mode Mode) (expr ast.Expr, err error)

Parameters & Attributes

NameTypeDescription
fset*token.FileSetNon-nil file set that records source positions.
filenamestringSource filename used in diagnostics.
srcanySource input; for ParseFile, nil reads filename, otherwise string, bytes or io.Reader.
modeModeBit flags controlling the scanner or parser.

Return Value

(expr ast.Expr, err error)

Parses expression input and records positions in a non-nil file set.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/parser"  "go/token")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  s := token.NewFileSet()  x, e := parser.ParseExprFrom(s, "expr.go", "x+1", parser.SkipObjectResolution)  check(e)  fmt.Println(s.Position(x.Pos()))}
Go Reference