蓝山帝景
要将AST转换为源格式,可以使用go / printer程序包。示例(另一示例的改编形式)package mainimport ( "go/parser" "go/printer" "go/token" "os")func main() { // src is the input for which we want to print the AST. src := `package mainfunc main() { println("Hello, World!")}` // Create the AST by parsing src. fset := token.NewFileSet() // positions are relative to fset f, err := parser.ParseFile(fset, "", src, 0) if err != nil { panic(err) } printer.Fprint(os.Stdout, fset, f)}输出:package mainfunc main() { println("Hello, World!")}