On Thu, Jan 9, 2020 at 10:22 AM <ju...@sqreen.io> wrote: > Hello, > > I am looking for a way to keep to original token positioning but I cannot > find how to avoid it when using `go/printer` or `go/format`. > Basically, how to make this example print the same string that was parsed: > https://play.golang.org/p/dI7WcevQJAZ > In the general case, this is not possible because the `go/ast <https://golang.org/pkg/go/ast/>` package types drop the positions of some tokens. (e.g. the `range` token in a `for := range` loop <https://golang.org/pkg/go/ast/#RangeStmt>) I'm reasonably certain code is only likely to round-trip (via ` go/format.Node <https://golang.org/pkg/go/format/#Node>`) if it was previously formatted with the same version's `gofmt` (or `go/format.Source <https://golang.org/pkg/go/format/#Source>`).
Based on the documentation on `go/printer <https://golang.org/pkg/go/printer/>`'s `Fprint <https://golang.org/pkg/go/printer/#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. > > Thanks for your help! > Julio > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/fbf8e5b6-f237-470c-9267-246132a2e0bc%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/fbf8e5b6-f237-470c-9267-246132a2e0bc%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CANrC0Bhf%2B0kN%3DpP7_fT%2BGFGfykS4G%3DawZSHufmGHQCw-y8B4FQ%40mail.gmail.com.