import (
"os"
"path/filepath"
+ "runtime"
+ "sync"
"cmd/go/internal/base"
"cmd/go/internal/cfg"
func runFmt(cmd *base.Command, args []string) {
gofmt := gofmtPath()
+ procs := runtime.GOMAXPROCS(0)
+ var wg sync.WaitGroup
+ wg.Add(procs)
+ fileC := make(chan string, 2*procs)
+ for i := 0; i < procs; i++ {
+ go func() {
+ defer wg.Done()
+ for file := range fileC {
+ base.Run(str.StringList(gofmt, "-l", "-w", file))
+ }
+ }()
+ }
for _, pkg := range load.Packages(args) {
// Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package,
// not to packages in subdirectories.
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
- base.Run(str.StringList(gofmt, "-l", "-w", files))
+ for _, file := range files {
+ fileC <- file
+ }
}
+ close(fileC)
+ wg.Wait()
}
func gofmtPath() string {