Use `go fmt` to keep Go source in a consistent style before sharing it. It is an alias for the separate `gofmt` tool, which offers additional options. Make formatting part of your normal coding workflow rather than a cleanup task at the end. section 6.1.1
A practical formatting loop:
- Run `go fmt` in the directory containing the files you want to format. An IDE can also format on save. section 1.5 section 6.1.1
- To inspect proposed changes without changing a file, run `gofmt -d myfile.go`. Version control can help review or undo applied changes. section 6.1.1
-- myfile.go.orig
+++ myfile.go
@@ -1,16 +1,13 @@
-
-
package main
import "fmt"
import "os"
-
func main() {
- fmt.Println( "checking for environment variable
CLEANUP") #1
-
- if envvar := os.GetEnv("CLEANUP"); envvar != "" {
- fmt.Println(" did not find it, value is:", envvar); #2
- }
+ fmt.Println("checking for environment variable
CLEANUP") #3
+
+ if envvar := os.GetEnv("CLEANUP"); envvar != "" {
fmt.Println(" did not find it, value is:", env
var) #4
+ }
}
The diff shows excess spaces and blank lines removed, indentation made consistent, and unnecessary semicolons dropped. Formatting still leaves some choices, such as separate import declarations, to the developer. section 6.1.1


