google code style is the same as gofmt in go codes?

189 views
Skip to first unread message

Moein Halvaei

unread,
Sep 13, 2022, 3:55:30 PM9/13/22
to golang-nuts
When I read codes from https://cs.opensource.google/go, indents are 2 spaces and when using gofmt indents are 2 tabs. Why?

David Finkel

unread,
Sep 13, 2022, 4:46:19 PM9/13/22
to Moein Halvaei, golang-nuts
On Tue, Sep 13, 2022 at 3:55 PM Moein Halvaei <moein...@gmail.com> wrote:
When I read codes from https://cs.opensource.google/go, indents are 2 spaces and when using gofmt indents are 2 tabs. Why?
Google's Codesearch defaults to matching Google's C++ style guide rather than Go's recommended style: https://google.github.io/styleguide/cppguide.html#Spaces_vs._Tabs 
To be clear: gofmt uses an 8-spaces per tab, not two tabs.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/3b43a2be-8001-4f53-a952-417b6a909bd9n%40googlegroups.com.

Axel Wagner

unread,
Sep 13, 2022, 5:40:26 PM9/13/22
to David Finkel, Moein Halvaei, golang-nuts
On Tue, Sep 13, 2022 at 10:46 PM David Finkel <david....@gmail.com> wrote:
On Tue, Sep 13, 2022 at 3:55 PM Moein Halvaei <moein...@gmail.com> wrote:
When I read codes from https://cs.opensource.google/go, indents are 2 spaces and when using gofmt indents are 2 tabs. Why?
Google's Codesearch defaults to matching Google's C++ style guide rather than Go's recommended style: https://google.github.io/styleguide/cppguide.html#Spaces_vs._Tabs 
To be clear: gofmt uses an 8-spaces per tab, not two tabs.

gofmt doesn't determine how many spaces are tab stops aligned to at all. That's a property of your editor, IDE or code browser and usually configurable. That's part of the appeal of using tabs for indentation and why spaces are better for alignment.
 

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/3b43a2be-8001-4f53-a952-417b6a909bd9n%40googlegroups.com.

--
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...@googlegroups.com.

David Finkel

unread,
Sep 13, 2022, 6:03:14 PM9/13/22
to Axel Wagner, Moein Halvaei, golang-nuts
On Tue, Sep 13, 2022 at 5:39 PM Axel Wagner <axel.wa...@googlemail.com> wrote:


On Tue, Sep 13, 2022 at 10:46 PM David Finkel <david....@gmail.com> wrote:
On Tue, Sep 13, 2022 at 3:55 PM Moein Halvaei <moein...@gmail.com> wrote:
When I read codes from https://cs.opensource.google/go, indents are 2 spaces and when using gofmt indents are 2 tabs. Why?
Google's Codesearch defaults to matching Google's C++ style guide rather than Go's recommended style: https://google.github.io/styleguide/cppguide.html#Spaces_vs._Tabs 
To be clear: gofmt uses an 8-spaces per tab, not two tabs.

gofmt doesn't determine how many spaces are tab stops aligned to at all. That's a property of your editor, IDE or code browser and usually configurable. That's part of the appeal of using tabs for indentation and why spaces are better for alignment.
Well, that's partially true.
There are a few places where gofmt aligns tokens after indentation where the alignment doesn't quite work out correctly if the tab-stop interval isn't exactly 8 characters.
The `go/printer` package's Config includes a Tabwidth field for a reason (it's actually passed to a TabWriter to do the alignment).

(I didn't realize before that the go/printer package actually underlies the implementation of go/format and cmd/gofmt -- I had assumed that it was a reimplementation like go/ast vs the compiler-internal ast)

Axel Wagner

unread,
Sep 13, 2022, 6:24:17 PM9/13/22
to David Finkel, Moein Halvaei, golang-nuts
TIL. Do you have an example?

FWIW, I can think of a simpler reason why the Config has a Tabwidth field - to use it to decide how many spaces to use for a "tab". Which seems to be at least part of what it's used for. I agree that the use of a TabWriter seems to indicate that more is going on.

Dominik Honnef

unread,
Sep 13, 2022, 9:16:13 PM9/13/22
to Axel Wagner, David Finkel, Moein Halvaei, golang-nuts
gofmt's output does not depend on the displayed width of a tab.
Indentation uses tabs, alignment within a line uses spaces, and there is
no alignment directly following indentation.

Note that go/printer allows for configurations other than the one used
by go/format / gofmt. It allows indenting with spaces and aligning with
tabs; the tab width matters for at least one of those non-standard
formats.

As far as I can tell, the tab width specified in go/format is
irrelevant, and setting it to other values doesn't break any of the
tests.

Reply all
Reply to author
Forward
0 new messages