Fundamentals 7 min read

Tabs vs Spaces: Why Go Chooses Tabs and Others Favor Spaces

The article examines the long‑standing debate between tabs and spaces for code indentation, citing a massive GitHub analysis that shows spaces dominate most languages while Go uniquely enforces tabs, and explains the technical, historical, and practical reasons behind these preferences and how IDEs can enforce consistent styles.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Tabs vs Spaces: Why Go Chooses Tabs and Others Favor Spaces

Tabs vs Spaces Conflict

Google engineer Felipe Hoffa analyzed 400,000 repositories (about 1 billion files, 14 TB) on GitHub, excluding duplicates and tiny files, and found that spaces are far more popular than tabs for indentation in most languages.

Chart showing space vs tab usage across languages
Chart showing space vs tab usage across languages

The analysis was motivated by a transition from Java to Go, where the data shows that while Java and most languages favor spaces, Go almost exclusively uses tabs, prompting the question why.

Tab Indentation Style

Most Java style guides recommend using four spaces instead of a tab. A tab occupies eight character positions, so replacing a tab with four spaces reduces file size and improves visual consistency.

Four spaces are commonly used as the indentation unit; the exact construction (spaces vs tabs) is unspecified, but tabs are defined as exactly eight spaces.

IDE settings (e.g., Eclipse, IntelliJ IDEA) can be configured to automatically convert tabs to spaces on formatting.

Illustration of indentation guidelines in Python
Illustration of indentation guidelines in Python

Python requires consistent indentation to define block structure. Although Python does not enforce tabs or spaces, mixing them causes syntax errors; the convention is four spaces.

Why Spaces Are Generally Recommended

Team coding standards usually adopt four spaces as a replacement for tabs. IDEs provide automatic conversion, making it easy to enforce.

Tabs have ASCII code 9, spaces 32. A tab may appear as eight spaces in some editors but can render differently elsewhere, leading to misaligned code when viewed with different settings.

Spaces occupy a single character width regardless of editor, ensuring consistent appearance across environments, which is crucial for collaborative development and precise alignment of comments.

Why Go Is an Exception

Go includes a built‑in formatter, gofmt, which automatically formats code on save and enforces a single style across the ecosystem.

The official gofmt documentation states: “Gofmt formats Go programs. It uses tabs for indentation and blanks for alignment. Alignment assumes that an editor is using a fixed‑width font.”

Consequently, Go recommends tabs for indentation and spaces only for alignment, unless a special need arises.

The rationale is that a tab’s width can vary between editors, while a space is always one column; the key is to stay consistent within a project.

Tabs are dedicated indentation characters.

They allow developers with different visual preferences to adjust tab width without changing the source.

Using tabs prevents “half‑indent” issues when copying code between files with different space counts.

By fixing indentation to tabs, Go eliminates many formatting disputes, though the broader community still debates the merits of tabs versus spaces.

Gocoding standardsgofmtindentationtabsSpaces
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.