英文:
How to use rewrite rule to replace tabs with spaces in gofmt tool?
问题
gofmt
命令支持使用-r
标志在格式化代码时使用重写规则。如何使用它将制表符缩进替换为空格缩进?
英文:
gofmt
command supports -r flag to use rewrite rule during formatting the code. How to use it to replace tabs indentation with the spaces indentation?
答案1
得分: 5
Go的格式要求使用制表符缩进,这是由gofmt
强制执行的。还可以查看导致这一决策的原因:https://code.google.com/p/go/issues/detail?id=7101
英文:
Go's format forced by gofmt
dictates to use tab indentation.
Also see the decision lead to it:
https://code.google.com/p/go/issues/detail?id=7101
答案2
得分: 3
> 命令 gofmt
>
> 使用 -r 标志指定的重写规则必须是以下形式的字符串:
>
> pattern -> replacement
>
> pattern 和 replacement 都必须是有效的 Go 表达式。
制表符和空格字符不是有效的 Go 表达式。这样是行不通的。
> Go 编程语言
>
> Alan A. A. Donovan & Brian W. Kernighan
>
> ISBN: 978-0134190440
>
> gopl.io
>
> Go 对代码格式化有着明确的立场。gofmt 工具将代码重写为标准格式,go 工具的 fmt 子命令将 gofmt 应用于指定包中的所有文件,或者默认情况下应用于当前目录中的文件。本书中的所有 Go 源文件都经过了 gofmt 处理,你应该养成同样的习惯,对自己的代码也进行格式化。通过强制声明标准格式,消除了关于琐事的无谓争论,更重要的是,它使得各种自动化源代码转换成为可能,如果允许任意格式化,这将是不可行的。
始终使用 gofmt 进行代码格式化。
英文:
> Command gofmt
>
> The rewrite rule specified with the -r flag must be a string of the
> form:
>
> pattern -> replacement
>
> Both pattern and replacement must be valid Go expressions.
The tab and space characters are not valid Go expressions. It won't work.
> The Go Programming Language
>
> Alan A. A. Donovan & Brian W. Kernighan
>
> ISBN: 978-0134190440
>
> gopl.io
>
> Go takes a strong stance on code formatting. The gofmt tool rewrites
> code into the standard format, and the go tool’s fmt subcommand
> applies gofmt to all the files in the specified package, or the ones
> in the current directory by default. All Go source files in the book
> have been run through gofmt, and you should get into the habit of
> doing the same for your own code. Declaring a standard format by fiat
> eliminates a lot of pointless debate about trivia and, more
> importantly, enables a variety of automated source code
> transformations that would be infeasible if arbitrary formatting were
> allowed.
Always use gofmt code formatting.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论