## 格式化

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}
type T struct {    name string // 对象名    value int // 对象值}

gofmt will line up the columns:

gofmt 会将它按列对齐为：

type T struct {    name    string // name of the object    value   int    // its value}
type T struct {    name    string // 对象名    value   int    // 对象值}

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.
缩进  我们使用制表符（tab）缩进，gofmt 默认也使用它。在你认为确实有必要时再使用空格。行的长度  Go 对行的长度没有限制，别担心打孔纸不够长。如果一行实在太长，也可进行折行并插入适当的 tab 缩进。括号  比起 C 和 Java，Go 所需的括号更少：控制结构（if、for 和 switch）在语法上并不需要圆括号。此外，操作符优先级处理变得更加简洁，因此    x<<8 + y<<16  正表述了空格符所传达的含义。