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
| Name | Type | Description |
|---|---|---|
| fset | *token.FileSet | Non-nil file set that records source positions. |
| filename | string | Source filename used in diagnostics. |
| src | any | Source input; for ParseFile, nil reads filename, otherwise string, bytes or io.Reader. |
| mode | Mode | Bit 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()))}