GCI, ein Tool, das die Importreihenfolge von Go-Paketen steuert und sie immer deterministisch macht.
Das gewünschte Ausgabeformat ist hochgradig konfigurierbar und ermöglicht eine individuellere Formatierung als goimport
.
GCI betrachtet einen auf AST basierenden Importblock wie folgt:
Doc
Name Path Comment
Alle Kommentare bleiben unverändert, mit Ausnahme der isolierten Kommentarblöcke.
Die isolierten Kommentarblöcke sehen wie folgt aus:
import (
"fmt"
// this line is isolated comment
// those lines belong to one
// isolated comment blocks
"github.com/daixiang0/gci"
)
GCI unterteilt alle Importblöcke in verschiedene Abschnitte und unterstützt jetzt sechs Abschnittstypen:
Die Priorität ist Standard > Standard > Benutzerdefiniert > Leer > Punkt > Alias > Localmodule, alle Abschnitte werden darin alphabetisch sortiert. Standardmäßig werden Leer-, Punkt- und Alias-Abschnitte nicht verwendet und die entsprechenden Zeilen landen in den anderen Gruppen.
Alle Importblöcke verwenden ein TAB( t
) als Einzug.
Seit Version 0.9.0 setzt GCI den C-Importblock immer an erster Stelle.
Notiz :
nolint
ist auf Abschnittsebene schwer zu handhaben, GCI betrachtet es als einen einzelnen Kommentar.
Die lokale Modulerkennung erfolgt durch Lesen des Modulnamens aus der Datei go.mod
in dem Verzeichnis, in dem gci
aufgerufen wird . Das heisst:
gci
von einem Modulstammverzeichnis (z. B. einem Verzeichnis mit go.mod
) aufgerufen wird.gci
aus einem Verzeichnis aufgerufen wird, go.work
enthält (obwohl er funktionieren würde, wenn er aus einem der Module im Arbeitsbereich aufgerufen würde). So laden Sie die höchste verfügbare Release-Version herunter und installieren sie:
go install github.com/daixiang0/gci@latest
Sie können auch eine bestimmte Version angeben, zum Beispiel:
go install github.com/daixiang0/[email protected]
Jetzt bietet GCI zwei Befehlszeilenmethoden, hauptsächlich aus Gründen der Abwärtskompatibilität.
GCI unterstützt drei Betriebsmodi
Notiz
Seit v0.10.0 können die Flags
-s
und--section
nur mehrmals verwendet werden, um mehrere Abschnitte anzugeben. Beispielsweise könnten Sie vorher-s standard,default
verwenden, aber jetzt müssen Sie-s standard -s default
verwenden. Diese bahnbrechende Änderung ermöglicht es dem Projekt, die Angabe mehrerer benutzerdefinierter Präfixe zu unterstützen. (Siehe bitte unten.)
$ gci print -h
Print outputs the formatted file. If you want to apply the changes to a file use write instead !
Usage:
gci print path... [flags]
Aliases:
print, output
Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for print
-s, --section stringArray Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias > localmodule. The default value is [standard,default].
standard - standard section that Go provides officially, like " fmt "
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
localmodule: localmodule section, contains all imports from local packages
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
$ gci write -h
Write modifies the specified files in-place
Usage:
gci write path... [flags]
Aliases:
write, overwrite
Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section stringArray Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias > localmodule. The default value is [standard,default].
standard - standard section that Go provides officially, like " fmt "
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
localmodule: localmodule section, contains all imports from local packages
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
$ gci list -h
Prints the filenames that need to be formatted. If you want to show the diff use diff instead, and if you want to apply the changes use write instead
Usage:
gci list path... [flags]
Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for list
-s, --section stringArray Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias > localmodule. The default value is [standard,default].
standard - standard section that Go provides officially, like " fmt "
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
localmodule: localmodule section, contains all imports from local packages
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
$ gci diff -h
Diff prints a patch in the style of the diff tool that contains the required changes to the file to make it adhere to the specified formatting.
Usage:
gci diff path... [flags]
Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for diff
-s, --section stringArray Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias > localmodule. The default value is [standard,default].
standard - standard section that Go provides officially, like " fmt "
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
localmodule: localmodule section, contains all imports from local packages
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
Gci enables automatic formatting of imports in a deterministic manner
If you want to integrate this as part of your CI take a look at golangci-lint.
Usage:
gci [-diff | -w rite] [--local localPackageURLs] path... [flags]
gci [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
diff Prints a git style diff to STDOUT
help Help about any command
list Prints filenames that need to be formatted to STDOUT
print Outputs the formatted file to STDOUT
write Formats the specified files in-place
Flags:
-d, --diff display diffs instead of rewriting files
-h, --help help for gci
-l, --local strings put imports beginning with this string after 3rd-party packages, separate imports by comma
-v, --version version for gci
-w, --write write result to (source) file instead of stdout
Use " gci [command] --help " for more information about a command.
Notiz ::
Der alte Stil ist nur für lokale Tests gedacht und wird nicht mehr unterstützt. Bitte verwenden Sie den neuen Stil. golangci-lint
verwendet ebenfalls den neuen Stil.
Führen Sie gci write -s standard -s default -s "prefix(github.com/daixiang0/gci)" main.go
aus und Sie werden die folgenden Fälle bearbeiten:
package main
import (
"golang.org/x/tools"
"fmt"
"github.com/daixiang0/gci"
)
Zu
package main
import (
"fmt"
"golang.org/x/tools"
"github.com/daixiang0/gci"
)
package main
import (
"fmt"
go "github.com/golang"
"github.com/daixiang0/gci"
)
Zu
package main
import (
"fmt"
go "github.com/golang"
"github.com/daixiang0/gci"
)
package main
import (
"fmt"
go "github.com/golang"
_ "github.com/golang/blank"
. "github.com/golang/dot"
"github.com/daixiang0/gci"
_ "github.com/daixiang0/gci/blank"
. "github.com/daixiang0/gci/dot"
)
Zu
package main
import (
"fmt"
go "github.com/golang"
"github.com/daixiang0/gci"
_ "github.com/daixiang0/gci/blank"
_ "github.com/golang/blank"
. "github.com/daixiang0/gci/dot"
. "github.com/golang/dot"
)
package main
import (
testing "github.com/daixiang0/test"
"fmt"
g "github.com/golang"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
)
Zu
package main
import (
"fmt"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
testing "github.com/daixiang0/test"
g "github.com/golang"
)
Angenommen, dies wird im Stammverzeichnis dieses Repos ausgeführt (d. h. dort, wo github.com/daixiang0/gci
ein lokales Modul ist).
package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
Zu
package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
Name
und Path
stehtPath
und Comment
stehtgoimports
und dann GCI).