In Go, if you want to save the AST (Abstract Syntax Tree) representation of a file rather than just the original source code, you need to serialize the AST structure itself. The printer.Fprint function you're using only reconstructs the original source code from the AST, not the AST structure.
Here are a few approaches to save the AST representation:
1. Using go/ast and encoding/gob (binary format)
package main
import (
"bytes"
"encoding/gob"
"go/ast"
"go/parser"
"go/token"
"os"
)
func saveAST(file *ast.File, filename string) error {
// Create a buffer to hold the serialized data
var buf bytes.Buffer
// Create a gob encoder
enc := gob.NewEncoder(&buf)
// Encode the AST
if err := enc.Encode(file); err != nil {
return err
}
// Write to file
return os.WriteFile(filename, buf.Bytes(), 0644)
}
func main() {
// Example usage
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "example.go", nil, parser.ParseComments)
if err != nil {
panic(err)
}
if err := saveAST(file, "example.ast.gob"); err != nil {
panic(err)
}
}
2. Using JSON (human-readable format)
package main
import (
"encoding/json"
"go/ast"
"go/parser"
"go/token"
"os"
)
func saveASTAsJSON(file *ast.File, filename string) error {
// Convert AST to JSON
data, err := json.MarshalIndent(file, "", " ")
if err != nil {
return err
}
// Write to file
return os.WriteFile(filename, data, 0644)
}
func main() {
// Example usage
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "example.go", nil, parser.ParseComments)
if err != nil {
panic(err)
}
if err := saveASTAsJSON(file, "example.ast.json"); err != nil {
panic(err)
}
}
3. Using go/ast and go/format (if you want formatted source)
If you actually want the formatted source code (which is what your original code does), then your approach is correct, but you might want to use format.Node for better formatting:
package main
import (
"bytes"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
)
func saveFormattedSource(file *ast.File, filename string) error {
var buf bytes.Buffer
if err := format.Node(&buf, token.NewFileSet(), file); err != nil {
return err
}
return os.WriteFile(filename, buf.Bytes(), 0644)
}
func main() {
// Example usage
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "example.go", nil, parser.ParseComments)
if err != nil {
panic(err)
}
if err := saveFormattedSource(file, "example.formatted.go"); err != nil {
panic(err)
}
}
Important Notes:
- The AST contains pointers and cyclic references, so not all serialization methods will work.
gob handles this well.
- If you need to reconstruct the AST later, you'll need to implement a corresponding
LoadAST function.
- The JSON approach might lose some information due to unexported fields in the AST nodes.
- For most use cases, saving the formatted source code (your original approach) is sufficient unless you specifically need the AST structure.
Choose the method that best fits your needs - binary serialization with gob is most complete but not human-readable, JSON is human-readable but might lose some information, and formatted source is what you'd typically want for most use cases.