3 ms·
Honestly, that's not a very good representation of what gofmt can do. This is a much better example: http://research.swtch.com/gofmt http://research.swtch.com/
by NateDad 14y ago
Honestly, that's not a very good representation of what gofmt can do.
This is a much better example: http://research.swtch.com/gofmt http://research.swtch.com/gofmt
gofmt -w -r 'x[i:len(x)] -> x[i:]' *.go
Note that this is not a regular expression... it's looking for go expressions that match that pattern.
So, for example, this will catch this:
a := charts[start:len(charts)]
and convert it to this:
a := charts[start:]
- chimeracoder 14y agoAlso, a regex will replace matching strings in incorrect places (like a string literal that happens to contain Go code). You can design your regex around this, but at some point, you'll end up just reinventing a Go parser inside a hideous, unmaintainable regex. So you might as well just use gofmt.
- danieldk 14y agoTrue, it's much more than simple search and replace, since it produces an AST, which provides more powerful refactoring. It's also not very spectacular, since it's what Java IDEs have done for ages, plus incremental compiling :). (Or code analysis tools such as Sonatype.) Go is just playing catch-up here. The one thing they did very well is dictating the code style with gofmt. You may not like it, but at least there is once standard. Though, one wonders how many developers and businesses use whatever layout Eclipse or Netbeans use out of the box ;).