173

Is there a standard Google Go coding conventions document somewhere that sets whether tabs or spaces are preferred for indentation in Go source code?

What is the official recommendation, if any?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • 2
    https://medium.com/@hoffa/400-000-github-repositories-1-billion-files-14-terabytes-of-code-spaces-or-tabs-7cfe0b5dd7fd#.o1d3cmm5r – Brad Werth Aug 31 '16 at 20:13

2 Answers2

263

The official recommendation is formatting your code with

go fmt

or using the gofmt command directly

gofmt -w .

You can read more about it here on the golang.org blog, or from the Effective go document:

Indentation
We use tabs for indentation and gofmt emits them by default. Use spaces only if you must.

ANisus
  • 74,460
  • 29
  • 162
  • 158
  • 3
    Yes, tabs for indentation is the official recommendation. You _can_ use space indentation as @Intermernet points out, but it is not the recommendation. – ANisus Sep 30 '13 at 12:56
  • 37
    @ErikAllik, actually, the official position is not "use tabs" but rather "use `go fmt` on your code before submitting it" (note there's also `gofmt`). What I'm trying to strees is that Go done this *right* not by just declaring a policy but by adopting a tool which enforces it. This is uncommon, so try to bend your head around this idea and adopt `go fmt`. Note that there are even automated solutions (for instance, official Go plugin for Vim supports the `:Fmt` command, IIRC). – kostix Sep 30 '13 at 13:17
  • 2
    @ErikAllik, [if only you were right about "no coding-convention arguments"...](https://groups.google.com/d/msg/golang-nuts/rzLzp_Z74ik/gc9xnEP0G40J) ;-) – kostix Sep 30 '13 at 14:00
  • Just an observation: go fmt will keep space indentations inside declared strings (both "" and ``) but will indent code elsewhere with tabs. – marcio Nov 29 '14 at 13:29
  • 1
    Even though it's enforced by the tool, it is still important to know so one can configure how to *represent tabs* in their editor of choice. (For instance, Emacs users could set `tab-width` in their `go-mode-hook`.) – eold Feb 04 '16 at 17:45
  • 9
    I think it's worth noting that adopting/requiring a tool does not stop all arguments, but it does mean that instead of people getting mad at you for your opinionated choices, people get mad at you for accepting the tool (and at the tool makers for their opinionated choices). – mtraceur Apr 10 '18 at 00:11
28

EDIT 2: he original answer at the bottom is now incorrect. The correct section of the linked source file (current 30/12/2019) is:

Gofmt formats Go programs. It uses tabs for indentation and blanks for alignment. Alignment assumes that an editor is using a fixed-width font.

Thanks to TehSphinX for pointing this out!

ALL INFO BELOW THIS LINE IS NOW INCORRECT

EDIT: The original answer at the bottom is now incorrect. The correct section of the linked source file (current 25/07/2014) is:

Gofmt formats Go programs.
It uses tabs (width = 8) for indentation and blanks for alignment.

Original answer (deprecated):

Formatting control flags:
    -comments=true
        Print comments; if false, all comments are elided from the output.
    -tabs=true
        Indent with tabs; if false, spaces are used instead.
    -tabwidth=8
        Tab width in spaces.
Intermernet
  • 18,604
  • 4
  • 49
  • 61
  • 4
    `-tabs` and `-tabwidth` are [gone](https://code.google.com/p/go/issues/detail?id=7101) (and `-comments` is no longer documented at least; haven't looked more closely) – William Jul 24 '14 at 21:32
  • 2
    @billisphere Thanks, I've updated the answer with the relevant section of the linked file. – Intermernet Jul 25 '14 at 10:33
  • 7
    When you use tab characters there is no such thing as using a particular tab width, so the (former) tabwidth option has no effect and the "width=8” part of the updated text should be removed. Tab width is configured in the viewer. This error also exists in the linked source. – thomasrutter May 22 '17 at 07:41
  • 2
    This is outdated. There is no more mention of a tabwidth in the documentation of the `gofmt` command. It has been removed here: https://github.com/golang/go/commit/25b51810014cefb8dba31321fcf40eb1a008fc3e – TehSphinX Dec 11 '19 at 09:10