Why does gofmt by default use an 8 space indent?

15,413 views
Skip to first unread message

JohnGB

unread,
Mar 7, 2014, 9:01:19 AM3/7/14
to golan...@googlegroups.com
I can't understand what the reasoning was for an 8 space indent.  I realise that this doesn't affect the functioning of any code, but it does affect whether the code fits on a screen and hence the code readability.  Even "A tour of Go" uses 4 space indents instead of 8.

So what is the advantage of 8 space indents that isn't adequately addressed by 4 space indents?

Aram Hăvărneanu

unread,
Mar 7, 2014, 9:12:21 AM3/7/14
to JohnGB, golang-nuts
As mentioned on IRC already, Go doesn't use 8-space indents, it uses a tab.

What's the point in asking questions in one place if when you're
unsatisfied with the answer you just asked them in another place?

--
Aram Hăvărneanu

John Beckett

unread,
Mar 7, 2014, 9:16:34 AM3/7/14
to Aram Hăvărneanu, golang-nuts
Thanks for the answer, but I don't understand exactly how I was supposed to know that this had been asked on IRC.  Is there some way to check what's been asked on IRC before that I am missing?


Aram Hăvărneanu

unread,
Mar 7, 2014, 9:19:06 AM3/7/14
to John Beckett, golang-nuts
I apologise, someone asked on IRC about this 5 minute before you posted
here, I wrongly assumed it was you.

--
Aram Hăvărneanu

John Beckett

unread,
Mar 7, 2014, 9:21:47 AM3/7/14
to Aram Hăvărneanu, golang-nuts
@Aram: honest mistake, so no problem.

Asked another way though.  Why does gofmt default to an 8 space tab width?  The gist of the question wasn't about tabs vs. spaces, but more about the default width of an indent (regardless of what it was).

Péter Szilágyi

unread,
Mar 7, 2014, 9:30:19 AM3/7/14
to John Beckett, Aram Hăvărneanu, golang-nuts
A tab is expanded to whatever amount of spaces your editor/viewer uses, it is completely unrelated to gofmt. I for example use 2 spaces per tab.


--
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.
For more options, visit https://groups.google.com/d/optout.

John Beckett

unread,
Mar 7, 2014, 10:38:35 AM3/7/14
to Péter Szilágyi, Aram Hăvărneanu, golang-nuts
@Péter I realise that, which is why I use 4 per tab.  However the documentation seems to all use 8, and I'd like to believe that there must have been a reason for that.  

In my experience, whenever there is something that I find strange, but that much more experienced programmers use, that there is a good reason for it.  I'm simply curious as to what the reason is.

Nate Finch

unread,
Mar 7, 2014, 10:42:06 AM3/7/14
to golan...@googlegroups.com, Aram Hăvărneanu
The width of a tab is based on whatever is displaying the tab.  You're evidently using an editor that has it set to 8.  Set it to something else if you don't like 8.  Tabs have no inherent width on their own, it's totally up to the thing displaying the tab.  

John Beckett

unread,
Mar 7, 2014, 11:02:40 AM3/7/14
to Nate Finch, golang-nuts, Aram Hăvărneanu
@Nate My tab with is set to 4, and that is how I see all code in my editor (Sublime Text).  However, if I look at any of the Go documentation online (http://golang.org/pkg/image/#Image for example), the tab width is 8.  I'm not complaining about it being 8 or asking how to change it to something else.  I'm trying to understand why it is 8 by default on the documentation etc.  


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/93t0LWvHrKM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

frou

unread,
Mar 7, 2014, 11:19:03 AM3/7/14
to golan...@googlegroups.com, Péter Szilágyi, Aram Hăvărneanu
Accepting there must be some value, I'm fairly sure that 8 has simply been the canonical one on Unix. Although 4 is used often in various contexts today, I don't think history would agree that it is the "default".

Jan Mercl

unread,
Mar 7, 2014, 11:21:32 AM3/7/14
to John Beckett, Nate Finch, golang-nuts, Aram Hăvărneanu
On Fri, Mar 7, 2014 at 5:02 PM, John Beckett <jgbe...@gmail.com> wrote:
> I'm trying to understand why it is 8 by default on the documentation
> etc.

For example, a 110 Bd mechanical Teletype* interpreted the tab
character by advancing the printing position to the next column
divisible by 8 (zero based index).

(*) The primary programming interface at my first job. And the
switches on the input panel. The later was of course sometimes faster
and/or more convenient.

-j

Nate Finch

unread,
Mar 7, 2014, 11:38:25 AM3/7/14
to golan...@googlegroups.com, Nate Finch, Aram Hăvărneanu
Ahh, see, that's a lot more specific:  Why is the tab width 8 on golang.org?  

And the answer is likely that whoever wrote the script to run godoc for golang.org was a fan of 8 spaces.  

Note that godoc, which is what generates the HTML documentation that golang.org uses, has a flag for tab width, which defaults to 4 spaces.  Someone evidently overrode that default for golang.org.  I think old school coders are more used to 8 character indents, and many of the prominent names on the Go team are old school coders (or think like them).

Wes Freeman

unread,
Mar 7, 2014, 11:40:49 AM3/7/14
to Jan Mercl, John Beckett, Nate Finch, golang-nuts, Aram Hăvărneanu
This also bothered me, since I'm used to using spaces on all my code, and I usually do just 3. I set up user stylesheets (http://userstyles.org/) in chrome to set tabs to 3 spaces in github.com and golang.org (only works on the examples, though, not the inline docs part, which has hard spaces in <pre> tags).

Wes

Andy Balholm

unread,
Mar 7, 2014, 11:48:00 AM3/7/14
to golan...@googlegroups.com, Nate Finch, Aram Hăvărneanu
On Friday, March 7, 2014 8:38:25 AM UTC-8, Nate Finch wrote:
Ahh, see, that's a lot more specific:  Why is the tab width 8 on golang.org?  

I suspect that godoc is emitting actual tab characters, and your browser is displaying them as 8 spaces. That's what's happening in the playground at least. In most HTML contexts, a tab is equivalent to one space, but in elements that pay attention to whitespace, I would expect the default to be 8 spaces. Maybe it can be changed with CSS.

Wes Freeman

unread,
Mar 7, 2014, 5:04:09 PM3/7/14
to golang-nuts
I gave it another 15 minutes and was indeed able to get the userstyle working in the inline examples as well--not sure why I had trouble before. In case anyone is interested:

RickyS

unread,
Mar 10, 2014, 8:14:37 AM3/10/14
to golan...@googlegroups.com
If you use the dumb output commands, you get 8-space tabs.  On windows, that's 'type' and 'print'.  On linux, that's 'cat' and sometimes 'print'.
If you use a tab setting that is not 8 then when you mix spaces and tabs (for an indent-width of like 2, 3, or 4), then it's all messed up when printed by the 'dumb' programs.  Indentation is reversed, in becomes out and vice versa.

Who uses the dumb software?  Code reviewers, auditors, testers, managers, newbies, courts, and so on.

So my view has been that indent-width must be a pleasant 2 or 4, but tab-width is already set in stone, and must be 8.  But now that the world is more software-savvy than it used to be, perhaps I should revisit this...
 
    R

Péter Szilágyi

unread,
Mar 10, 2014, 8:46:59 AM3/10/14
to RickyS, golang-nuts
Don't mix tab and space. Use one or the other, never both. Then it doesn't matter who views your code and with what. Go prefers tabs (gofmt does it for you automatically), which you can size to whatever width you like in the editor. This keeps things consistent, and you can view it at whatever width it pleases you. 


--
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.

Lars Seipel

unread,
Mar 10, 2014, 5:49:19 PM3/10/14
to John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
On Fri, Mar 07, 2014 at 04:38:35PM +0100, John Beckett wrote:
> In my experience, whenever there is something that I find strange, but that
> much more experienced programmers use, that there is a good reason for it.

There probably is one. Whenever you have a problem fitting your code on
the screen with an indentation equivalent to 8 spaces per level your
code is probably way too nested anyway and you should most likely change
it. Make it easier to read by removing the amount of state a reader has
to track to understand your code.

Aside from that, everybody knows a tab is 8 spaces wide. ☺

Lars

Rob Pike

unread,
Mar 10, 2014, 5:54:05 PM3/10/14
to Lars Seipel, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
"Aside from that, everybody knows a tab is 8 spaces wide."

Except that's not true. It's measured in units of the width of a
numeral. Only in a fixed-width font (who uses those nowadays? Wow,
flashback to a teletype!) are those the same thing.

-rob

Aram Hăvărneanu

unread,
Mar 10, 2014, 6:18:45 PM3/10/14
to Rob Pike, Lars Seipel, John Beckett, Péter Szilágyi, golang-nuts
> Except that's not true. It's measured in units of the width of a
> numeral. Only in a fixed-width font (who uses those nowadays? Wow,
> flashback to a teletype!) are those the same thing.

Amusingly, I switched from a variable width font to a fixed width font
because of Go (gofmt assumes fixed width font in order to align struct
fields).

I miss the variable width font, but it seems I have a slight case of
OCD which prevents me going back.

--
Aram Hăvărneanu

Lars Seipel

unread,
Mar 10, 2014, 6:23:02 PM3/10/14
to Rob Pike, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
Ah, that width-of-a-numeral bit brings enlightenment.

Lars

Michael Jones

unread,
Mar 10, 2014, 6:37:46 PM3/10/14
to Lars Seipel, Rob Pike, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
And numerals have fixed horizontal pitch only in fonts designed for setting tables. 

If you use a more generous font such as Minion Pro you'll find alternate sets of numerals with fixed pitch and with variable widths ('1' narrower than '0'), and with aligned baselines ("lining") and with with old-style varying vertical positions. The full cross product exists in some fonts. The choices look like this:

Inline image 1
The fixed & lining (lower-right) choice is that of the typewriter and the computer spreadsheet.

Michael "The would-be typographer" Jones

On Mon, Mar 10, 2014 at 3:23 PM, Lars Seipel <lars....@gmail.com> wrote:
Ah, that width-of-a-numeral bit brings enlightenment.

Lars
--
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.
For more options, visit https://groups.google.com/d/optout.



--
Michael T. Jones | Chief Technology Advocate  | m...@google.com |  +1 650-335-5765

Rob Pike

unread,
Mar 10, 2014, 6:42:41 PM3/10/14
to Michael Jones, Lars Seipel, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
Even to my humanist eyes, a numeral one that looks like a serifed capital eye looks wrong.

-rob

Bakul Shah

unread,
Mar 10, 2014, 6:52:29 PM3/10/14
to Rob Pike, Lars Seipel, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
> Only in a fixed-width font (who uses those nowadays?

Almost everyone using vim/nvi.

Michael Jones

unread,
Mar 10, 2014, 7:04:36 PM3/10/14
to Rob Pike, Lars Seipel, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
On Mon, Mar 10, 2014 at 3:42 PM, Rob Pike <r...@golang.org> wrote:
Even to my humanist eyes, a numeral one that looks like a serifed capital eye looks wrong.

Your taste in numerals follows the Nepalese tradition. A very popular point of view, certainly.

Jeremy Jackins

unread,
Mar 10, 2014, 7:06:14 PM3/10/14
to golan...@googlegroups.com, Lars Seipel, John Beckett, Péter Szilágyi, Aram Hăvărneanu
Rob, do you simply live with oddly aligned Go code? Or is there some trick to seeing nicely aligned structs with a variable-width font?

Dan Kortschak

unread,
Mar 10, 2014, 7:20:26 PM3/10/14
to Michael Jones, Rob Pike, Lars Seipel, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
On Mon, 2014-03-10 at 16:04 -0700, Michael Jones wrote:
> Your taste in numerals follows the Nepalese tradition

Not sure why that article focuses on the digit being Nepali since it's a
glyph from Devanagari which is a much more widely used script.

Rob Pike

unread,
Mar 10, 2014, 7:25:51 PM3/10/14
to Dan Kortschak, Michael Jones, Lars Seipel, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
I just live with unaligned comments. Life is too short to care about
the topic. Let the computer do the aligning - as it does. And if it
looks unaligned on my screen sometimes - I don't care. Why should I?
Code is not a form to be filled in, at least not since about 1965.

-rob

Matt Harden

unread,
Mar 11, 2014, 11:07:06 PM3/11/14
to Rob Pike, Dan Kortschak, Michael Jones, Lars Seipel, John Beckett, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
Time for the obligatory reference to Elastic Tabstops. :-)


John Beckett

unread,
Mar 12, 2014, 7:33:38 AM3/12/14
to Matt Harden, Rob Pike, Dan Kortschak, Michael Jones, Lars Seipel, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
@Matt Thanks, that is really interesting and an almost ideal solution (for me at least).  

Is there a way of getting gofmt to use elastic tab stops when formatting?

Dan Kortschak

unread,
Mar 12, 2014, 8:08:03 AM3/12/14
to John Beckett, Matt Harden, Rob Pike, Michael Jones, Lars Seipel, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
On Wed, 2014-03-12 at 12:33 +0100, John Beckett wrote:
> Is there a way of getting gofmt to use elastic tab stops when
> formatting?

go/printer can align using tabs (in the Config struct), so you can just
fork a gofmt that does that for import into an editor that you have with
elastic tab stops (then change back for public consumption).

JussiJ

unread,
Mar 12, 2014, 8:40:14 AM3/12/14
to golan...@googlegroups.com, Matt Harden, Rob Pike, Dan Kortschak, Michael Jones, Lars Seipel, Péter Szilágyi, Aram Hăvărneanu
On Wednesday, March 12, 2014 10:33:38 PM UTC+11, JohnGB wrote:
 
Is there a way of getting gofmt to use elastic tab stops when formatting?

So can you define what is an elastic tab stop?

chris dollin

unread,
Mar 12, 2014, 8:56:06 AM3/12/14
to JussiJ, golang-nuts, Matt Harden, Rob Pike, Dan Kortschak, Michael Jones, Lars Seipel, Péter Szilágyi, Aram Hăvărneanu
On 12 March 2014 12:40, JussiJ <jus...@zeusedit.com> wrote:

So can you define what is an elastic tab stop?

Just a few messages back Matt made an "obligatory reference" to
 

 Chris

--
Chris "npt obliged, but willing" Dollin

Lars Seipel

unread,
Mar 13, 2014, 5:25:00 AM3/13/14
to John Beckett, Matt Harden, Rob Pike, Dan Kortschak, Michael Jones, Péter Szilágyi, Aram Hăvărneanu, golang-nuts
On Wed, Mar 12, 2014 at 12:33:38PM +0100, John Beckett wrote:
> Is there a way of getting gofmt to use elastic tab stops when formatting?

The issue isn't so much gofmt but the almost nonexistent support in
editors. Some would probably need a major overhaul to properly support
elastic tabstops.

Dobrosław Żybort

unread,
Mar 13, 2014, 11:07:38 AM3/13/14
to golan...@googlegroups.com, Matt Harden, Rob Pike, Dan Kortschak, Michael Jones, Lars Seipel, Péter Szilágyi, Aram Hăvărneanu
But gofmt already use elastic tab stops for comments after variables declarations (in groups):
http://play.golang.org/p/eRShCFYc6v (click "Format")

Matt Harden

unread,
Mar 17, 2014, 7:26:23 PM3/17/14
to Dobrosław Żybort, golang-nuts, Rob Pike, Dan Kortschak, Michael Jones, Lars Seipel, Péter Szilágyi, Aram Hăvărneanu
On Thu, Mar 13, 2014 at 10:07 AM, Dobrosław Żybort <matr...@gmail.com> wrote:
But gofmt already use elastic tab stops for comments after variables declarations (in groups):
http://play.golang.org/p/eRShCFYc6v (click "Format")

Not really. Elastic tabstops is about enabling variable width fonts to be used in text (and especially programming) while still lining up tabular columns. What gofmt does is to use spaces to align columns; a method described in http://nickgravgaard.com/elastictabstops/ as a "kludge".

Nico

unread,
Mar 18, 2014, 5:52:51 AM3/18/14
to Matt Harden, Dobrosław Żybort, golang-nuts, Rob Pike, Dan Kortschak, Michael Jones, Lars Seipel, Péter Szilágyi, Aram Hăvărneanu
I have to say that:

using variable-width fonts in programming is akin to using comic sans to
present the finding of the Higgs boson!

hollowa...@gmail.com

unread,
May 16, 2017, 1:10:42 AM5/16/17
to golang-nuts
Because it warns you that you're probably nesting too deep and should consider breaking them into smaller functions. It's more of a coding practice, not some language standards. Read #1 from https://www.kernel.org/doc/html/v4.10/process/coding-style.html


On Friday, March 7, 2014 at 10:01:19 PM UTC+8, JohnGB wrote:
I can't understand what the reasoning was for an 8 space indent.  I realise that this doesn't affect the functioning of any code, but it does affect whether the code fits on a screen and hence the code readability.  Even "A tour of Go" uses 4 space indents instead of 8.

So what is the advantage of 8 space indents that isn't adequately addressed by 4 space indents?

Rob Pike

unread,
May 16, 2017, 1:33:48 AM5/16/17
to hollowa...@gmail.com, golang-nuts
The indent character is tab, whose width is your decision. That's in large part why the indent character is tab: to let you choose how much to indent.

-rob


--
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+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages