parent
b3769056e7
commit
c55bc093be
|
@ -38,6 +38,13 @@ var (
|
||||||
ErrGoModEmptyModule = errors.New("go mod empty module")
|
ErrGoModEmptyModule = errors.New("go mod empty module")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// https://pkg.go.dev/sync#Pool
|
||||||
|
var bufPool = sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
return new(bytes.Buffer)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// stdPackages -> save std packages for later search
|
// stdPackages -> save std packages for later search
|
||||||
//
|
//
|
||||||
// moduleNames -> map path to its go.mod module name
|
// moduleNames -> map path to its go.mod module name
|
||||||
|
@ -287,12 +294,17 @@ func (ft *Formatter) formatImports(
|
||||||
// Second update
|
// Second update
|
||||||
dstFile.Decls[0].(*dst.GenDecl).Specs = formattedGenSpecs
|
dstFile.Decls[0].(*dst.GenDecl).Specs = formattedGenSpecs
|
||||||
|
|
||||||
var buf bytes.Buffer
|
b := bufPool.Get().(*bytes.Buffer)
|
||||||
if err := decorator.Fprint(&buf, dstFile); err != nil {
|
b.Reset()
|
||||||
|
defer bufPool.Put(b)
|
||||||
|
|
||||||
|
if err := decorator.Fprint(b, dstFile); err != nil {
|
||||||
return nil, fmt.Errorf("decorator: failed to fprint [%s]: %w", path, err)
|
return nil, fmt.Errorf("decorator: failed to fprint [%s]: %w", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
result := make([]byte, b.Len())
|
||||||
|
copy(result, b.Bytes())
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleName string) (map[string][]*dst.ImportSpec, error) {
|
func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleName string) (map[string][]*dst.ImportSpec, error) {
|
||||||
|
|
Loading…
Reference in New Issue