Indentation in Go: tabs or spaces?

∥☆過路亽.° 提交于 2019-12-03 06:26:04

问题


Is there a standard Google Go coding conventions document somewhere that sets whether tabs or spaces are preferred for indentation in Go source code? If not, what is the (statistically) more popular option?

  1. What is the official recommendation? (if any)
  2. What is the more popular choice?

回答1:


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:


According to http://golang.org/src/cmd/gofmt/doc.go the defaults are:

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.


来源:https://stackoverflow.com/questions/19094704/indentation-in-go-tabs-or-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!