How can I remove an unwanted import alias in Go?

huangapple go评论93阅读模式
英文:

How can I remove an unwanted import alias in Go?

问题

我在网上找到了一个非常有用的Go库,网址是https://github.com/deckarep/golang-set,它试图将Python的集合转换为Go语言。

通过使用Visual Studio Code,我最终成功导入了该库import "github.com/deckarep/golang-set",并在我的代码中调用了一个函数:

mySet := mapset.NewSet()

VS Code会自动识别别名并替换导入指令:

import mapset "github.com/deckarep/golang-set"

然而,作为一个觉得这些别名令人困惑的人,我试图将其删除,但这样做后,VS Code会将其从导入语句和我的代码中都删除。然后,VS Code告诉我:

> 未声明的名称:NewSet compiler(UndeclaredName)

NewSet(...)的包名也是package 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?

答案1

得分: 5

这里有几个要点:

  1. mapset 是包的名称。你可以通过查看包的源代码来确认。

  2. 虽然在语言层面上来说,导入别名并不是必需的,但为了清晰起见,添加了导入别名。因为包名(mapset)与导入路径(golang-set)不匹配,如果在导入语句中没有别名,就无法确定如何引用该包。这就是为什么别名很重要的原因。

  3. 你不能使用golang-set作为导入名称,因为在导入别名中不允许使用-字符。然而,如果你真的想要这样做,你可以通过显式地提供别名来使用golang_set或类似的名称:

    import golang_set "github.com/deckarep/golang-set"
    

    注意,这违反了命名约定,因为包名中不应该有_字符。但它仍然是有效的。

  4. 最佳实践是只使用mapset作为别名。它是所有可用选项中最不容易引起混淆的选项(这也是为什么它被自动选择的原因)。

英文:

Several things here:

  1. mapset is the package name. You can see this by looking at the package source code.

  2. 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.

  3. 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 use golang_set or similar, by explicitly providing this as your alias:

    import golang_set "github.com/deckarep/golang-set"
    

    Note that this goes against naming conventions, in that packages should not have _ characters in the name. But it should still be valid.

  4. 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).

huangapple
  • 本文由 发表于 2022年4月22日 19:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/71968358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定