Indentation in Go: tabs or spaces?

huangapple go评论83阅读模式
英文:

Indentation in Go: tabs or spaces?

问题

在Go源代码中,是否有一个标准的Google Go编码规范文档,规定了是使用制表符还是空格进行缩进?

如果有的话,官方推荐使用哪种方式?

英文:

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?

答案1

得分: 263

官方推荐使用以下方式格式化你的代码:

go fmt

或者直接使用 gofmt 命令:

gofmt -w .

你可以在 golang.org 的博客上这里了解更多信息,或者从 Effective go 文档中获取:

缩进
我们使用制表符进行缩进,默认情况下 gofmt 会生成制表符。只有在必要时才使用空格。

英文:

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.

答案2

得分: 28

EDIT 2: 底部的原始答案现在不正确。链接的源文件(当前日期为2019年12月30日)的正确部分如下:

>Gofmt 格式化 Go 程序。
它使用制表符进行缩进,使用空格进行对齐。
对齐假设编辑器使用的是等宽字体。

感谢 TehSphinX 指出这一点!

以下所有信息都已过时

EDIT: 底部的原始答案现在不正确。链接的源文件(当前日期为2014年7月25日)的正确部分如下:

>Gofmt 格式化 Go 程序。
它使用制表符(宽度为8)进行缩进,使用空格进行对齐。

原始答案(已弃用):

格式控制标志:
	-comments=true
		打印注释;如果为 false,则所有注释都将从输出中省略。
	-tabs=true
		使用制表符进行缩进;如果为 false,则使用空格。
	-tabwidth=8
		制表符宽度(以空格为单位)。
英文:

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.

huangapple
  • 本文由 发表于 2013年9月30日 20:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/19094704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定