In the general case, this is not possible because the `
go/ast` package types drop the positions of some tokens. (e.g. the `range` token in a
`for := range` loop)
I'm reasonably certain code is only likely to round-trip (via `
go/format.Node`) if it was previously formatted with the same version's `gofmt` (or `
go/format.Source`).
Based on the documentation on `
go/printer`'s `
Fprint` function, it doesn't seem to make any guarantees about matching the parsed format. From that doc:
Fprint "pretty-prints" an AST node to output. It calls Config.Fprint with default settings. Note that gofmt uses tabs for indentation but spaces for alignment; use format.Node (package go/format) for output that matches gofmt.
I wish I had more helpful advice.