vscode + go:找不到包,无法导入函数

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

vscode + go: cannot find package, cannot import function

问题

我有一个带有几个Go模块的VSCode工作区。我有一个顶级的go.work文件。
我已经安装了Go插件并安装了所有的依赖项。我在工作区中初始化了单独的模块,并运行了go use命令将所有模块添加到我的go.work文件中。

不幸的是,我没有意识到包通常被命名为"github.com/my-org/my-package/utils"之类的形式。所以我只有简单的模块名,比如xserviceyservice等。在这样一个模块文件夹内部,我有:

  • main.go <-- 顶部是 package main
  • go.mod
  • go.sum
  • /folder1
  • /folder1/utils.go <- 顶部是 package main + 包含函数DoSomething()

在main.go中:我如何调用DoSomething()函数?VSCode不允许我导入或调用它,无论我在路径或模块名方面做什么。而且它是同一个模块的一部分。

请注意,这个问题是因为这里给出的示例:https://go.dev/doc/code 不起作用。也就是说,我似乎无法在带有顶级go.work文件的VSCode中使其工作。

更新

根据下面的评论,要求提供一个可重现的示例。我正在整理它,并注意到当我在模块中的所有地方使用package main时,它无法编译。
vscode + go:找不到包,无法导入函数

当我按照目录结构并在包名中反映出来时,根据下面的答案它可以工作(即使foo.go属于package utils)。

英文:

I have a vscode workspace with a couple of go modules. I have a top-level go.work file.
I have installed the go plugin and installed all its dependencies. I have initialized separate modules in the workspace and have ran go use to add all the modules to my go.work file.

Unfortunately I didn't realize packages were generally named like "github.com/my-org/my-package/utils" etc. So I just have simple module names like xservice and yservice etc. Inside one such module folder, I have:

  • main.go <-- package main at the top
  • go.mod
  • go.sum
  • /folder1
  • /folder1/utils.go <- package main at the top + contains function DoSomething()

Inside main.go: how do I call DoSomething()? vscode will not let me import it or call it, not matter what I do in terms of paths or module names. And it is part of the same module.

Please note that this question is because the example given here: https://go.dev/doc/code does not work. As in, I can't seem to get it to work in vscode with a top-level go.work file.

Update

Was asked for a reproducible example per comment below. I was putting it together and saw that when I use package main everywhere, in the module, it wouldn't compile
vscode + go:找不到包,无法导入函数

When I followed the directory structure and reflected that in the package names per the answer below it worked (i.e. make foo.go belong to package utils instead).

答案1

得分: 1

vs code不是一个集成开发环境(IDE),而是一个文本编辑器。
经常会发生一些小的配置错误导致整个Intellisense崩溃的情况。

因此,我建议首先将“View->Problems”的数量减少到0。

在Go语言中,所有的包都是文件夹。因此,我认为你的IDE在抱怨包的配置错误。

/folder1/utils.go <- 在顶部的包名为main

在我看来,utils.go应该包含package folder1

项目结构与文件夹结构相同。当你使用xserviceyservice时,我期望找到两个相应名称的文件夹。
参考示例

为了实现这一点,我建议利用Go的强大功能,而不是手动初始化。当正确执行时,Go可以为你生成一个模块

当你在一个module中时,你可以简单地访问任何子包。

示例代码如下:

main.go <- 在顶部的包名为"main"
go.mod <- 模块名为"whatever"
/folder1/utils.go <- 在顶部的包名为"folder1",包含函数DoSomething()

然后你可以在main函数中简单地调用folder1.DoSomething()
根据Go的逻辑,你有一个名为"whatever"的module,它有一个可执行的main函数和一个名为"folder1"的package,其中包含DoSomething函数。

英文:

vs code is not an IDE, it is a text editor.
it happens very often, that some minor misconfiguration breaks down the whole Intellisense.

that' why i suggest to first start with reducing the amount of "View->Problems" to 0.

in go all packages are folders. for that reason i assume your IDE complains about package misconfiguration

> /folder1/utils.go <- package main at the top

in my opinion utils.go should contain package folder1

the project structure is equal the folder structure. when you use xservice and yservice i expect to find 2 folders with the respective names.
see an example

to achieve that, i suggest to use the power of go and not try to initialize stuff manually. when executed properly, go can generate a module for you.

when you are in a module, you can simply access any subpackage.

the example would be like:

main.go &lt;- package &quot;main&quot; at the top
go.mod &lt;- module &quot;whatever&quot;
/folder1/utils.go &lt;- package &quot;folder1&quot; at the top, contains function DoSomething()

then you can simply call folder1.DoSomething() in main.
on the logic of go, you have a module called "whatever" which has an executable main and a package "folder1" with DoSomething.

huangapple
  • 本文由 发表于 2023年4月11日 03:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980121.html
匿名

发表评论

匿名网友

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

确定