3 ms·
I think you are missing the point of gofmt, which is to establish a one-to-one correspondence between the source and the AST. This way, you can parse the source
by ngrilly 9y ago
I think you are missing the point of gofmt, which is to establish a one-to-one correspondence between the source and the AST. This way, you can parse the source to an AST, modify the AST, and reconvert the AST to source, and you have a guarantee that the resulting diff only touches the modified parts.
- pcwalton 9y agogofmt doesn't do that. It doesn't touch user-inserted line breaks (not to mention comments!)
- ngrilly 9y agoI don't get your point. Let's say I have this program (which was goformatted): package main import ( "fmt" ) // commenting... // commenting... func main() { fmt.Println("hello", "world") fmt.Println( "hello", "world", ) } And I had log.Print("test") at the end of main(): package main import ( "fmt" ) // commenting... // commenting... func main() { fmt.Println("hello", "world") fmt.Println( "hello", "world", ) log.Print("test") } Then I run goimports to automatically update the import directive (goimports parses the source to build an AST, then modifies the AST, then reconverts it to source code): package main import ( "fmt" "log" ) // commenting... // commenting... func main() { fmt.Println("hello", "world") fmt.Println( "hello", "world", ) log.Print("test") } Everything is kept exactly as-is, except the added line "log" in the import directive. This round-trip mechanism is the main benefit of gofmt.