Formatting
source ↗Formatting
Formatting issues are the most contentious but the least consequential. People can adapt to different formatting styles but it’s better if they don’t have to, and less time is devoted to the topic if everyone adheres to the same style. The problem is how to approach this Utopia without a long prescriptive style guide.
With Go we take an unusual
approach and let the machine
take care of most formatting issues.
The gofmt program
(also available as go fmt, which
operates at the package level rather than source file level)
reads a Go program
and emits the source in a standard style of indentation
and vertical alignment, retaining and if necessary
reformatting comments.
If you want to know how to handle some new layout
situation, run gofmt; if the answer doesn’t
seem right, rearrange your program (or file a bug about gofmt),
don’t work around it.
As an example, there’s no need to spend time lining up
the comments on the fields of a structure.
Gofmt will do that for you. Given the
declaration
type T struct {
name string // name of the object
value int // its value
}
gofmt will line up the columns:
type T struct {
name string // name of the object
value int // its value
}
All Go code in the standard packages has been formatted with gofmt.
Some formatting details remain. Very briefly:
Indentation
: We use tabs for indentation and gofmt emits them by default.
Use spaces only if you must.
Line length : Go has no line length limit. Don’t worry about overflowing a punched card. If a line feels too long, wrap it and indent with an extra tab.
Parentheses
: Go needs fewer parentheses than C and Java: control structures (if,
for, switch) do not have parentheses in their syntax.
Also, the operator precedence hierarchy is shorter and clearer, so
x<<8 + y<<16
: means what the spacing implies, unlike in the other languages.