如何删除GO中不需要的进口别名?
我发现网络中非常有用的GO库 https://github.com/deckarep/golang/golang-set < /a>试图使用Python端口的设置。
多亏了Visual Studio代码,我最终通过导入库导入“ github.com/deckarep/golang-set”
in ,并在我的代码中调用函数:
mySet := mapset.NewSet()
vs代码自动识别别名并替换,导入指令:
import mapset "github.com/deckarep/golang-set"
但是,作为一个发现这些别名令人困惑的人,我试图将其删除,但是这样做,Vscode将其从导入语句和我的代码中删除。然后,VS代码告诉我:
未宣布的名称:Newset Compiler(undeclaredName)
noreferrer“> newset( ...)也是软件包mapset
。所以我认为我可以简单地删除它。但是它行不通。
我还试图与其他第三方软件包类似工作,并以存储库的名称调用功能:
mySet := golang-set.NewSet()
这也导致错误。由于连字符,还是我正在监督其他东西,在这里不可能删除别名吗?
I found that very useful Go library in the web https://github.com/deckarep/golang-set that tries to port Python sets to Go.
Thanks to Visual Studio Code I eventually got it to work by importing the library import "github.com/deckarep/golang-set"
and calling a function in my code:
mySet := mapset.NewSet()
VS Code automatically recognizes the alias and replaces the import directive:
import mapset "github.com/deckarep/golang-set"
However, being someone who finds those aliases confusing, I was trying to remove it but doing so, VSCode removes it from both the import statements and my code. VS Code then tells me:
undeclared name: NewSet compiler(UndeclaredName)
The package name from NewSet(...) is also package mapset
. So I thought I could simply remove it. But it does not work.
I also tried to work analogously to other 3rd party packages and call the functions by the repository's name:
mySet := golang-set.NewSet()
This also leads to an error. Is the removing of the alias not possible here due to the hyphen maybe or am I overseeing something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几件事:
mapset
是软件包名称。您可以通过查看软件包源代码。虽然在这种情况下,从语言的角度来看,导入别名并不是严格需要的,但为了清楚起见,由于软件包名称(
mapset
>)不匹配导入路径(>
) Golang-set
)。没有导入语句中的别名,就无法说明如何引用软件包。这就是为什么要在那里很重要的原因。您不能使用
Golang-set
作为导入名称,因为导入别名不允许-
字符。但是,如果您真的愿意,则可以通过将其作为您的别名来使用golang_set
或类似方法:请注意,这与命名约定有关,在该软件包中不应具有名称中的
_
字符。但这仍然应该有效。最佳实践将只是使用
mapset
作为别名。这是所有可用选项中最不令人困惑的(这就是为什么自动选择的原因)。Several things here:
mapset
is the package name. You can see this by looking at the package source code.While the import alias, in this case, is not strictly needed from a language standpoint, it's added for clarity, since the package name (
mapset
) does not match the import path (golang-set
). Without the alias in the import statement, there's no way to tell how the package is referenced. This is why it's important for it to be there.You cannot use
golang-set
as your import name, becuase the-
character is not permitted in an import alias. However, if you really want to, you could usegolang_set
or similar, by explicitly providing this as your alias:Note that this goes against naming conventions, in that packages should not have
_
characters in the name. But it should still be valid.Best practice would be just to use
mapset
as the alias. It's the least confusing of all the available options (which is why it's automatically selected).