GCI,一个控制 Go 包导入顺序并使其始终具有确定性的工具。
所需的输出格式是高度可配置的,并且允许比goimport
更多的自定义格式。
GCI 考虑基于 AST 的导入块如下:
Doc
Name Path Comment
除了孤立的评论块之外,所有评论都将保持原样。
孤立的评论块如下所示:
import (
"fmt"
// this line is isolated comment
// those lines belong to one
// isolated comment blocks
"github.com/daixiang0/gci"
)
GCI 将所有导入块拆分为不同的部分,现在支持六种部分类型:
优先级是标准 > 默认 > 自定义 > 空白 > 点 > 别名 > 本地模块,所有部分按字母顺序排序。默认情况下,不使用空白、点和别名部分,并且相应的行最终位于其他组中。
所有导入块都使用一个制表符 ( t
) 作为缩进。
从 v0.9.0 开始,GCI 总是将 C 导入块放在第一位。
笔记:
nolint
在节级别很难处理,GCI 会将其视为单个注释。
本地模块检测是通过从调用gci
的目录中的go.mod
文件中读取模块名称来完成的。这意味着:
go.mod
目录)调用gci
时,此模式有效go.work
目录调用gci
时(尽管如果从工作区中的模块之一调用,它会起作用) 要下载并安装可用的最高发行版本 -
go install github.com/daixiang0/gci@latest
您还可以指定特定版本,例如:
go install github.com/daixiang0/[email protected]
现在GCI提供了两种命令行方式,主要是为了向后兼容。
GCI支持三种操作模式
笔记
从 v0.10.0 开始,
-s
和--section
标志只能多次使用来指定多个部分。例如,以前可以使用-s standard,default
,但现在必须使用-s standard -s default
。这一重大更改使该项目可以支持指定多个自定义前缀。 (请参阅下文。)
$ 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.
笔记::
旧样式仅用于本地测试,将被弃用,请使用新样式, golangci-lint
也使用新样式。
运行gci write -s standard -s default -s "prefix(github.com/daixiang0/gci)" main.go
,您将处理以下情况:
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"
)
假设这是在此存储库的根目录上运行(即github.com/daixiang0/gci
是本地模块)
package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
到
package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
Name
和Path
之间只有一个空格Path
和Comment
之间只有一个空格goimports
再使用GCI)