表达式的定义
type Expr interface {Node// contains filtered or unexported methods}type BadExpr struct{ ... }type BinaryExpr struct{ ... }type CallExpr struct{ ... }type Expr interface{ ... }type ExprStmt struct{ ... }type IndexExpr struct{ ... }type KeyValueExpr struct{ ... }type ParenExpr struct{ ... }type SelectorExpr struct{ ... }type SliceExpr struct{ ... }type StarExpr struct{ ... }type TypeAssertExpr struct{ ... }type UnaryExpr struct{ ... }
我们以ast.BinaryExpr表达的二元算术表达式开始,因为加减乘除四则运算是我们最熟悉的表达式结构:
func main() {expr, _ := parser.ParseExpr(`1+2*3`)ast.Print(nil, expr)}-------------------------------output-------------------------------0 *ast.BinaryExpr {1 . X: *ast.BasicLit {2 . . ValuePos: 13 . . Kind: INT4 . . Value: "1"5 . }6 . OpPos: 27 . Op: +8 . Y: *ast.BinaryExpr {9 . . X: *ast.BasicLit {10 . . . ValuePos: 311 . . . Kind: INT12 . . . Value: "2"13 . . }14 . . OpPos: 415 . . Op: *16 . . Y: *ast.BasicLit {17 . . . ValuePos: 518 . . . Kind: INT19 . . . Value: "3"20 . . }21 . }22 }

