简体   繁体   中英

How to use rewrite rule to replace tabs with spaces in gofmt tool?

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 ?

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

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 AA 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM