U
U
uvelichitel2022-01-19 19:37:10
go
uvelichitel, 2022-01-19 19:37:10

Can ast.File be compiled?

Packages go/ast, go/paser provide an opportunity to get a parse tree, AST code. It can be edited and then poured back into the source code using go/printer. Is it possible to compile it without translating it back into source code? It seems that this is what the regular compiler does - first it builds the AST, then it proceeds to the next stage of compilation. Whether such possibility is given to the developer - to finish compiling ast.File?
package main

import (
  "go/printer"
  "go/parser"
  "go/token"
)
func main() {
  src := `
package main
func main() {
  println("Hello, World!")
}
`
  fset := token.NewFileSet()
    // дерево получить просто
  f, _ := parser.ParseFile(fset, "", src, 0)
  // просто его вывести обратно в исходный код
  printer.Fprint(os.Stdout, fset, f)
    // а можно ли его сбилдить?
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question