Skip to content

go/printer: Fprintf uses different settings than go fmt  #16963

Closed
@kevinburke

Description

@kevinburke

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.7

What operating system and processor architecture are you using (go env)?

All

What did you do?

If I write a Go file with go/printer.Fprintf, structs get printed like this - I've replaced the tab characters with \t so it's clear what is going on.

type User struct{
\tName\t\tstring
\tLongConfig\tuint
}

If you immediately run go fmt ./... on the file, some of the tabs are replaced by spaces:

type User struct{
\tName        string
\tLongConfig  uint
}

go/printer.Fprintf uses the following default printer.Config argument:

return (&Config{Tabwidth: 8}).Fprint(output, fset, node)

However, go fmt uses this config argument:

const (
    tabWidth    = 8
    printerMode = printer.UseSpaces | printer.TabIndent
)

// ...

res, err := format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})

What did you expect to see?

I expect the two "default" functions to print the file the same way

What did you see instead?

One prints with tabs in the middle and one doesn't. This led to a lot of thrash when running anything automated over the codebase; I noticed this first as part of the investigation for Shyp/bump_version#1.

Far more people run go fmt than write AST parsing/hacking functions, so it seems like the fix, if there is to be one, should be to have printer.Fprintf match the go fmt behavior.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions