程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了go 测试结果在 go: not find main module, but found .git/config in /Users/dp/Documents大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决go 测试结果在 go: not find main module, but found .git/config in /Users/dp/Documents?

开发过程中遇到go 测试结果在 go: not find main module, but found .git/config in /Users/dp/Documents的问题如何解决?下面主要结合日常开发的经验,给出你关于go 测试结果在 go: not find main module, but found .git/config in /Users/dp/Documents的解决方法建议,希望对你解决go 测试结果在 go: not find main module, but found .git/config in /Users/dp/Documents有所启发或帮助;

我刚刚开始使用 Golang,正在尝试运行单元测试。运行 go test 时,我最终从终端获得以下输出

go: cAnnot find main module,but found .git/config in /Users/dp/documents
    to create a module there,run:
    cd ../.. && go mod init

我的文件结构比较简单,如下

├── CARDS
│   ├── .vscode
│   ├── main.go
│   ├── test.go
│   ├── deck_test.go
│   └── my_cards.txt

deck_test.go 的内容是

package main

import "tesTing"

func TestNewDeck(t *tesTing.T) {
    d := newDeck()

    if len(d) != 52 {
        t.Errorf("Length is not 52,got %v",len(d))
    }

    if D[0] != "A of D" {
        t.Errorf("First card is not A of D,d[0])
    }

    if D[len(d)-1] != "K of C" {
        t.Errorf("Last card is not K of C,d[len(d)-1])
    }

}

任何见解都会有所帮助!

解决方法

然不是必需的,但通常建议当您在 Git(或其他 VCS)存储库中时使用 init,以便模块可以依靠您的 remote 信息来确定模块正确,例如:

git init
git remote add origin https://github.com/syntaqx/dacode
go mod init

允许 go mod 理解我的模块名称可能是 github.com/syntaqx/dacode

或者,我经常这样做是为了不需要以任何特定的顺序做事,你可以指定模块名称:

go mod init dacode # valid,but..
go mod init github.com/syntaqx/dacode # is generally better,because it describes my remote

通过指定它,模块可以在我所在的任何目录中进行初始化,而无需从代码中添加任何内容。

注意:特别是当您刚开始时,我强烈建议您使用与存储库相同的命名结构来命名您的模块。通过这样做,您允许以下命令:

go get github.com/syntaqx/dacode

无需自己进行任何内部操作即可正常运行。随着您对它们的工作原理了解得更多,您可以决定是要保持该惯例还是违背常规,但保持一致可能更明智。

希望这有帮助!

,

我没有使用 go 模块(我知道,我应该)。从旧的 Go 版本升级到 1.16 后,我收到了这个错误。来自发行说明:

请注意,Go 1.16 默认要求使用 Go 模块,现在,根据我们的 2020 年 Go 开发人员调查,96% 的 Go 开发人员已进行转换。我们最近添加了用于开发和发布模块的官方文档。

要仍然允许像以前一样工作,请将其更改为以前的样子。将此添加到您的 .bashrc 中:

export GO111MODULE=auto

大佬总结

以上是大佬教程为你收集整理的go 测试结果在 go: not find main module, but found .git/config in /Users/dp/Documents全部内容,希望文章能够帮你解决go 测试结果在 go: not find main module, but found .git/config in /Users/dp/Documents所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。