golang自动格式化导入语句工具插件goimports-reviser的使用
golang自动格式化导入语句工具插件goimports-reviser的使用
goimports-reviser是一个用于对Golang导入语句进行排序和格式化的工具,它可以按照3-4个分组(标准库、通用库、公司库和项目依赖)来组织导入语句,同时还提供代码格式化功能(不需要单独使用gofmt或goimports)。
主要功能
- 自动排序导入语句到不同分组
- 提供代码格式化功能(通过
-format
选项) - 移除未使用的导入(通过
-rm-unused
选项) - 为版本化包设置别名(通过
-set-alias
选项) - 自定义公司前缀(通过
-company-prefixes
选项)
安装方法
使用Go安装
go install -v github.com/incu6us/goimports-reviser/v3@latest
使用Brew安装
brew tap incu6us/homebrew-tap
brew install incu6us/homebrew-tap/goimports-reviser
使用Snap安装
snap install goimports-reviser
使用示例
基本用法
goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go
递归处理目录
goimports-reviser -rm-unused -set-alias -format -recursive reviser
处理多个目标
goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go ./pkg/...
代码示例
使用前
package testdata
import (
"log"
"github.com/incu6us/goimports-reviser/testdata/innderpkg"
"bytes"
"golang.org/x/exp/slices"
)
使用后
package testdata
import (
"bytes"
"log"
"golang.org/x/exp/slices"
"github.com/incu6us/goimports-reviser/testdata/innderpkg"
)
带有注释的导入
package testdata
import (
"fmt" // comments to the package here
)
格式化代码示例
使用前:
package main
func test(){
}
func additionalTest(){
}
使用后:
package main
func test(){
}
func additionalTest(){
}
配置选项
Usage of goimports-reviser:
-apply-to-generated-files
Apply imports sorting and formatting(if the option is set) to generated files. Generated file is a file with first comment which starts with comment '// Code generated'. Optional parameter.
-company-prefixes string
Company package prefixes which will be placed after 3rd-party group by default(if defined). Values should be comma-separated. Optional parameters.
-excludes string
Exclude files or dirs, example: '.git/,proto/*.go'.
-file-path string
Deprecated. Put file name as an argument(last item) of command line.
-format
Option will perform additional formatting. Optional parameter.
-imports-order string
Your imports groups can be sorted in your way.
std - std import group;
general - libs for general purpose;
company - inter-org or your company libs(if you set '-company-prefixes'-option, then 4th group will be split separately. In other case, it will be the part of general purpose libs);
project - your local project dependencies;
blanked - imports with "_" alias;
dotted - imports with "." alias.
Optional parameter. (default "std,general,company,project")
-list-diff
Option will list files whose formatting differs from goimports-reviser. Optional parameter.
-local string
Deprecated
-output string
Can be "file", "write" or "stdout". Whether to write the formatted content back to the file or to stdout. When "write" together with "-list-diff" will list the file name and write back to the file. Optional parameter. (default "file")
-project-name string
Your project name(ex.: github.com/incu6us/goimports-reviser). Optional parameter.
-recursive
Apply rules recursively if target is a directory. In case of ./... execution will be recursively applied by default. Optional parameter.
-rm-unused
Remove unused imports. Optional parameter.
-separate-named
Separate named imports from their group with a new line. Optional parameter.
-set-alias
Set alias for versioned package names, like 'github.com/go-pg/pg/v9'. In this case import will be set as 'pg "github.com/go-pg/pg/v9"'. Optional parameter.
-set-exit-status
set the exit status to 1 if a change is needed/made. Optional parameter.
-use-cache
Use cache to improve performance. Optional parameter.
-version
Show version.
在JetBrains IDE中配置
更多关于golang自动格式化导入语句工具插件goimports-reviser的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于golang自动格式化导入语句工具插件goimports-reviser的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
goimports-reviser 使用指南
goimports-reviser 是一个强大的 Go 语言工具,用于自动格式化导入语句并重新组织它们。它是标准 goimports
的增强版,提供了更多自定义选项和更灵活的导入分组方式。
安装
# 使用 go 安装
go install github.com/incu6us/goimports-reviser/v3@latest
# 或使用 brew (macOS)
brew install goimports-reviser
基本使用
格式化单个文件
goimports-reviser -file-path ./main.go
递归格式化目录
goimports-reviser -project-name github.com/your/project -recursive ./...
主要功能
- 自动添加缺失的导入
- 移除未使用的导入
- 按组重新组织导入语句
- 支持自定义导入分组
常用选项
选项 | 描述 |
---|---|
-file-path |
要格式化的文件路径 |
-project-name |
项目模块名称 (用于本地导入分组) |
-recursive |
递归处理目录 |
-format |
输出格式 (默认修改文件,可设为 diff 或 json ) |
-local |
本地包前缀 (多个用逗号分隔) |
-output |
输出到文件而非原地修改 |
-rm-unused |
是否移除未使用的导入 (默认 true) |
-set-alias |
为冲突导入设置别名 |
配置示例
1. 基本格式化
// 格式化前
import (
"fmt"
"os"
"github.com/some/external/pkg"
"yourproject/internal/utils"
)
// 格式化后 (假设项目名为 github.com/your/project)
import (
"fmt"
"os"
"github.com/some/external/pkg"
"github.com/your/project/internal/utils"
)
2. 自定义本地前缀
goimports-reviser -local "github.com/your/project,gitlab.com/other/local" -file-path main.go
3. 作为预提交钩子
在 .pre-commit-config.yaml
中添加:
repos:
- repo: https://github.com/incu6us/goimports-reviser
rev: v3.5.4
hooks:
- id: goimports-reviser
args: [-local=github.com/your/project]
Go 代码示例
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/your/project/internal/config"
"github.com/your/project/pkg/utils"
)
func main() {
fmt.Println("Properly formatted imports")
}
与 IDE 集成
VS Code 配置
在 settings.json
中添加:
{
"go.importsTool": "goimports-reviser",
"go.formatTool": "goimports-reviser",
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
}
高级用法
配置文件支持
创建 .goimports-reviser.toml
文件:
projectName = "github.com/your/project"
localPrefixes = ["github.com/your/project", "gitlab.com/other/local"]
format = "file"
recursive = true
skipGenerated = true
然后只需运行:
goimports-reviser ./...
常见问题
-
与 goimports 的区别:
- 更灵活的分组控制
- 支持本地包前缀定义
- 支持配置文件
- 支持递归处理
-
性能考虑:
- 对于大型项目,建议在 CI 中运行
- 可以使用
-skip-generated
跳过生成的文件
-
版本兼容性:
- 支持 Go 1.16+ 模块
- 与大多数 Go 工具链兼容
goimports-reviser 是保持 Go 代码导入整洁有序的强大工具,特别适合团队协作项目,可以强制执行一致的导入风格。