goimports 需要忽略供应商包

goimports needs to ignore vendor package

我正在尝试在我的项目中实施 dep。这一切都很好,但它也添加了一个供应商目录。我现在需要更新我的工具以忽略此目录,否则我出售的软件包将被修改,或者我收到误报的警告。我目前正在使用以下工具:

这些工具也在 CI 中使用。我确实想继续使用 goimports 自动格式化,但我愿意开始使用 gometalinter。我并不是真的在寻找使用 grep 和 find magic 的解决方案。

如何让这些工具忽略 vendor/

gometalinter 有一个“--vendor”标志来忽略供应商文件夹。该标志将所需的参数传递给底层工具以忽略该文件夹。

所以一种解决方案是仅使用 govet、golint 和 goimports 以及 gometalinter

gometalinter --disable-all --enable=vet --enable=golint --enable=goimports --vendor ./...

另一个解决方案可能是(从gist复制):

goimports -d $(find . -type f -name '*.go' -not -path "./vendor/*")

恕我直言,我更喜欢第一个解决方案。这样您就可以根据需要轻松添加其他 linters。

To exclude directories in your $GOPATH from being scanned for Go files, goimports respects a configuration file at $GOPATH/src/.goimportsignore which may contain blank lines, comment lines (beginning with '#'), or lines naming a directory relative to the configuration file to ignore when scanning. No globbing or regex patterns are allowed. Use the "-v" verbose flag to verify it's working and see what goimports is doing.

https://godoc.org/golang.org/x/tools/cmd/goimports

因此您可以将供应商目录添加到 $GOPATH/src/.goimportsignore 文件,例如:

github.com/foo/bar/vendor