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?
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?
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 go.dev 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.
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). –
Aney tab-width
in their go-mode-hook
.) –
Foretooth go fmt
uses tabs instead of spaces for indentation causes issues when I run git add --patch
and split chunks. Git changes tabs into spaces and then I get a: warning: 1 line adds whitespace errors.
Does anyone else have this issue with git add --patch
? –
Der 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.
-tabs
and -tabwidth
are gone (and -comments
is no longer documented at least; haven't looked more closely) –
Gotha gofmt
command. It has been removed here: github.com/golang/go/commit/… –
Carabiniere © 2022 - 2024 — McMap. All rights reserved.