To convert the AST to its original form, you can use the go / printer package .
Example (adapted form of another example )
package main
import (
"go/parser"
"go/printer"
"go/token"
"os"
)
func main() {
src := `
package main
func main() {
println("Hello, World!")
}
`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
panic(err)
}
printer.Fprint(os.Stdout, fset, f)
}
(also here )
Conclusion:
package main
func main() {
println("Hello, World!")
}
source
share