英文:
How to handle case sensitive import collisions
问题
我正在使用GoLang中的第三方库,该库的导入路径以前大小写不同。最初,一个字母是小写的,然后作者将其改为大写。
一些插件作者更新了他们的库,而其他人没有。与此同时,原始库的作者又将大小写改回了原样。
现在,由于大小写导入冲突,我的应用程序无法构建。
如何解决这个问题呢?
非常感谢。
英文:
I am using a third party library in GoLang that has previously had import paths in different case. Initially a letter was lower case then the author changed it to uppercase.
Some plugin authors updated their libraries and others didn't. In the meantime the original library author reverted the case change.
Now I find myself in a state where my application won't build due to to case import collisions.
How can one go about fixing this?
Many thanks
答案1
得分: 7
你可以将依赖项放入供应商目录(vendor),然后进入该目录并手动更改依赖项(可以尝试使用grep或sed命令来查找和替换依赖项)。
关于供应商目录的介绍,你可以参考这里:https://blog.gopheracademy.com/advent-2015/vendor-folder/
原始仓库仍然可以存在于你的GOPATH中,但是“修正后”的版本可以放在供应商目录中,编译器在链接依赖项时会首先查找该目录。
有许多供应商工具可供选择,我使用的是govendor。
编辑
正如mkopriva在评论中提到的,你可以使用gofmt
工具来重构导入名称:
gofmt -w -r '"path/to/PackageName" -> "path/to/packagename"' ./
gofmt -w -r 'PackageName.x -> packagename.x' ./
小写的单个字符标识符是通配符。
来自文档的说明:
使用-r标志指定的重写规则必须是以下形式的字符串:
pattern -> replacement
pattern和replacement都必须是有效的Go表达式。在pattern中,小写的单个字符标识符用作通配符,匹配任意子表达式;这些表达式将被替换为replacement中的相同标识符。
英文:
You could vendor the dependencies, and then go into the vendor/
directory and manually change (try grep
ing or sed
ing the dependency), the dependencies.
For an introduction to vendoring, try here, https://blog.gopheracademy.com/advent-2015/vendor-folder/
The original repo can still live in your GOPATH
, but the 'corrected' version can go in the vendor folder, in which the compiler will look first when linking dependencies.
There are many tools for vendoring, I use govendor
Edit
As mkopriva mentions in the comments, you can refactor import names using the gofmt
tool:
> gofmt -w -r '"path/to/PackageName" -> "path/to/packagename"' ./
>
> gofmt -w -r 'PackageName.x -> packagename.x' ./
The lowercase single character identifier is a wildcard.
from the docs
> The rewrite rule specified with the -r flag must be a string of the form:
>
> pattern -> replacement
>
> Both pattern and replacement must be valid Go expressions. In the pattern, single-character lowercase identifiers serve as wildcards matching arbitrary sub-expressions; those expressions will be substituted for the same identifiers in the replacement.
答案2
得分: 0
只是如果有人想知道为什么在你的项目中可能会出现这个错误:确保所有的导入路径使用小写或大写,而不是混合使用。
像这样:
one.go -> "github.com/name/app/login"
another.go -> "github.com/name/app/login"
而不是像这样:
one.go -> "github.com/name/app/Login"
another.go -> "github.com/name/app/login"
英文:
Just if anyone wonders why this error might occur in your project: Make sure all imports use either lowercase or uppercase path, but not mixed.
So like this:
one.go -> "github.com/name/app/login"
another.go -> "github.com/name/app/login"
And not like this:
one.go -> "github.com/name/app/Login"
another.go -> "github.com/name/app/login"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论