无法导入…(没有所需的模块提供包“…/utils/…”)

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

could not import ... (no required module provides package ".../utils/...")

问题

好的,以下是翻译的内容:

好的,我现在完全被难住了。我已经查看了这4个类似的问题/答案:

https://stackoverflow.com/questions/70842657/go-no-required-module-provides-package-mux-error

https://stackoverflow.com/questions/65716220/could-not-import-no-required-module-provides-package

https://stackoverflow.com/questions/60680470/could-not-import-local-modules-in-golang

https://stackoverflow.com/questions/58518588/vscode-could-not-import-golang-package

...它们基本上都重复了相同的答案,即运行go mod init <name>go mod tidy,我已经多次尝试过,但问题仍然存在。

我没有将这个包上传到 GitHub 存储库中,它只是存储在我 Windows 机器的本地位置。我已经运行了以下命令:

go mod init go_backtest

go mod tidy

...但我仍然在各个地方看到这些问题:

无法导入…(没有所需的模块提供包“…/utils/…”)

有趣的是,在"go_backtest/strategies"这一行下面没有红色下划线:

无法导入…(没有所需的模块提供包“…/utils/…”)

这是 go.mod 文件的内容:

module go_backtest

go 1.19

我对 golang 还是新手,请多多包涵,但我该如何使其“编译”和运行(并消除那些红色下划线!)

英文:

Ok I'm officially stumped. I've reviewed these 4 similar questions/answers:

https://stackoverflow.com/questions/70842657/go-no-required-module-provides-package-mux-error

https://stackoverflow.com/questions/65716220/could-not-import-no-required-module-provides-package

https://stackoverflow.com/questions/60680470/could-not-import-local-modules-in-golang

https://stackoverflow.com/questions/58518588/vscode-could-not-import-golang-package

...and they all basically repeat the same answer of running go mod init &lt;name&gt; and go mod tidy which I've done numerous times, same issue persists.

I do not have this package uploaded to a github repo, it's just stored locally on my Windows machine. I have run the commands:

go mod init go_backtest

go mod tidy

...and I am still seeing these all over the place:

无法导入…(没有所需的模块提供包“…/utils/…”)

what's interesting is there is NOT a red underline under the &quot;go_backtest/strategies&quot; line:

无法导入…(没有所需的模块提供包“…/utils/…”)

This is what the go.mod file looks like:

module go_backtest

go 1.19

I'm still a newbie to golang so be kind, but how can I get this thing to "compile" and run (and make those red underlines go away!)

答案1

得分: 2

utils目录中的文件应该属于同一个包utils。这些文件应该在开头有一行package utils

import语句用于导入一个包而不是一个文件。所以将

"go_backtest/utils/dataFuncs"
"go_backtest/utils/ga"

替换为

"go_backtest/utils"
英文:

The files in the utils directory should belong to the same package utils. Those files should have the line package utils at the very beginning.

And the import statement is used to import a package instead of a file. So replace

&quot;go_backtest/utils/dataFuncs&quot;
&quot;go_backtest/utils/ga&quot;

with

&quot;go_backtest/utils&quot;

huangapple
  • 本文由 发表于 2023年4月22日 13:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76078153.html
匿名

发表评论

匿名网友

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

确定