GCI, un outil qui contrôle l'ordre d'importation des packages Go et le rend toujours déterministe.
Le format de sortie souhaité est hautement configurable et permet un formatage plus personnalisé que ne le fait goimport
.
GCI considère un bloc d'importation basé sur AST comme ci-dessous :
Doc
Name Path Comment
Tous les commentaires resteront tels qu’ils étaient, à l’exception des blocs de commentaires isolés.
Les blocs de commentaires isolés comme ci-dessous :
import (
"fmt"
// this line is isolated comment
// those lines belong to one
// isolated comment blocks
"github.com/daixiang0/gci"
)
GCI divise tous les blocs d'importation en différentes sections et prend désormais en charge six types de sections :
La priorité est standard > par défaut > personnalisé > vide > point > alias > module local, toutes les sections sont triées par ordre alphabétique à l'intérieur. Par défaut, les sections vides, points et alias ne sont pas utilisées et les lignes correspondantes finissent dans les autres groupes.
Tous les blocs d'importation utilisent un TAB( t
) comme retrait.
Depuis la version 0.9.0, GCI place toujours le bloc d'importation C en premier.
Note :
nolint
est difficile à gérer au niveau de la section, GCI le considérera comme un seul commentaire.
La détection du module local se fait en lisant le nom du module dans le fichier go.mod
dans le répertoire où gci
est invoqué . Cela signifie:
gci
est invoqué depuis la racine d'un module (c'est-à-dire un répertoire contenant go.mod
)gci
est invoqué depuis un répertoire contenant go.work
(bien qu'il fonctionnerait s'il était invoqué depuis l'un des modules de l'espace de travail) Pour télécharger et installer la version la plus élevée disponible -
go install github.com/daixiang0/gci@latest
Vous pouvez également spécifier une version spécifique, par exemple :
go install github.com/daixiang0/[email protected]
GCI propose désormais deux méthodes de ligne de commande, principalement pour la compatibilité ascendante.
GCI prend en charge trois modes de fonctionnement
Note
Depuis la version 0.10.0, les indicateurs
-s
et--section
ne peuvent être utilisés que plusieurs fois pour spécifier plusieurs sections. Par exemple, vous pouviez utiliser-s standard,default
auparavant, mais vous devez maintenant utiliser-s standard -s default
. Ce changement radical permet au projet de prendre en charge la spécification de plusieurs préfixes personnalisés. (Veuillez voir ci-dessous.)
$ 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.
Note ::
L'ancien style est uniquement destiné aux tests locaux, sera obsolète, veuillez utiliser le nouveau style, golangci-lint
utilise également le nouveau style.
Exécutez gci write -s standard -s default -s "prefix(github.com/daixiang0/gci)" main.go
et vous gérerez les cas suivants :
package main
import (
"golang.org/x/tools"
"fmt"
"github.com/daixiang0/gci"
)
à
package main
import (
"fmt"
"golang.org/x/tools"
"github.com/daixiang0/gci"
)
package main
import (
"fmt"
go "github.com/golang"
"github.com/daixiang0/gci"
)
à
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"
)
à
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"
)
à
package main
import (
"fmt"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
testing "github.com/daixiang0/test"
g "github.com/golang"
)
En supposant que cela soit exécuté à la racine de ce dépôt (c'est-à-dire où github.com/daixiang0/gci
est un module local)
package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
à
package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
Name
et Path
dans un bloc d'importationPath
et Comment
dans un bloc d'importationgoimports
, puis utilisez GCI)