Re:Go and Versioning: Minimal Version Selection
by ngrilly on Thursday February 22, 2018 @06:23AM
(#16436829)
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.
[ Parent
| on HN ]
Re:Go and Versioning: Minimal Version Selection
by pcwalton on Thursday February 22, 2018 @01:53PM
(#16440233)
gofmt doesn't do that. It doesn't touch user-inserted line breaks (not to mention comments!)
Re:Go and Versioning: Minimal Version Selection
by ngrilly on Friday February 23, 2018 @07:04AM
(#16445802)
I 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.