mirror of
https://github.com/Binject/debug
synced 2026-06-08 10:28:33 +00:00
34 lines
552 B
Go
34 lines
552 B
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"go/format"
|
|
"go/parser"
|
|
"go/token"
|
|
"log"
|
|
)
|
|
|
|
func main() {
|
|
const expr = "(6+2*3)/4"
|
|
|
|
// parser.ParseExpr parses the argument and returns the
|
|
// corresponding ast.Node.
|
|
node, err := parser.ParseExpr(expr)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Create a FileSet for node. Since the node does not come
|
|
// from a real source file, fset will be empty.
|
|
fset := token.NewFileSet()
|
|
|
|
var buf bytes.Buffer
|
|
err = format.Node(&buf, fset, node)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(buf.String())
|
|
}
|