如何删除GO中不需要的进口别名?

发布于 2025-01-23 05:24:04 字数 953 浏览 0 评论 0原文

我发现网络中非常有用的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念﹏祤嫣 2025-01-30 05:24:04

这里有几件事:

  1. mapset是软件包名称。您可以通过查看软件包源代码

  2. 虽然在这种情况下,从语言的角度来看,导入别名并不是严格需要的,但为了清楚起见,由于软件包名称(mapset>)不匹配导入路径(> ) Golang-set)。没有导入语句中的别名,就无法说明如何引用软件包。这就是为什么要在那里很重要的原因。


  3. 您不能使用Golang-set作为导入名称,因为导入别名不允许-字符。但是,如果您真的愿意,则可以通过将其作为您的别名来使用golang_set或类似方法:

     导入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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文