为了账号安全,请及时绑定邮箱和手机立即绑定

错误消息:go: 在当前目录或任何父目录中找不到 go.mod 文件;

错误消息:go: 在当前目录或任何父目录中找不到 go.mod 文件;

Go
斯蒂芬大帝 2023-02-06 10:32:53
我正在尝试使用 go 运行单元测试。这些函数在主文件中正常工作。函数如下:func LoadLexicon(lexiconPath string) (map[string]string, error) {    m := make(map[string]string)    lexiconPath = strings.TrimSuffix(lexiconPath, "\n")    if lexiconPath == "nil" {        m["COME"] = "k V m"        m["WORDS"] = "w 3` d z"        m["MECCA"] = "m E k @"        return m, nil    }    readFile, err := os.Open(lexiconPath)    if err != nil {        fmt.Println(err)        return m, err    }    fileScanner := bufio.NewScanner(readFile)    fileScanner.Split(bufio.ScanLines)    var fileLines []string    for fileScanner.Scan() {        fileLines = append(fileLines, fileScanner.Text())    }    lex_words := make(map[string]string)    for _, line := range fileLines {        temp := strings.Split(line, "\t")        lex_words[strings.ToUpper(temp[0])] = temp[1]    }    return lex_words, err}但是当我运行单元测试时,func TestLoadLexicon(t *testing.T) {    tests := []struct {        n    string        want string    }{        {"COME", "k V m"},        {"WORDS", "w 3` d z"},        {"MECCA", "m E k @"},    }    for _, tc := range tests {        if got, _ := LoadLexicon("nil"); got[tc.n] != tc.want {            t.Errorf("got %s, want %s", got[tc.n], tc.want)        }    }}我收到这个错误`运行工具:/usr/local/go/bin/go test -timeout 30s -run ^TestLoadLexicon$go: 在当前目录或任何父目录中找不到 go.mod 文件;查看“去帮助模块”试运行于 29/08/2022 02:58:53 < `
查看完整描述

1 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

您需要将go.mod文件添加到项目的根目录。


使用模块来管理依赖关系。官方文档:https ://go.dev/blog/using-go-modules


例子:


go mod init project-name



go mod init example.com/project-name



go mod init github.com/you-user-name/project-name

在运行上述命令之一后,您可能需要使用 tidy 命令进行清理。


go mod tidy

将包导入 go 文件时使用上面的路径格式


例子:


import (

   // Import internal and external packages like this

   "github.com/you-user-name/project-name/package-name"


   // Import standard library packages the normal way

   "testing"

   "math/rand"

)


查看完整回答
反对 回复 2023-02-06
  • 1 回答
  • 0 关注
  • 363 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号