简体   繁体   English

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

[英]Error Message : go: go.mod file not found in current directory or any parent directory;

I am trying to run a unit test using go.我正在尝试使用 go 运行单元测试。 The functions work properly in the main file.这些功能在主文件中正常工作。 The function is given below: function如下:

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

}

But when I am running the unit test,但是当我运行单元测试时,

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)
        }
    }
}

I am getting this error ` Running tool: /usr/local/go/bin/go test -timeout 30s -run ^TestLoadLexicon$我收到此错误`运行工具:/usr/local/go/bin/go test -timeout 30s -run ^TestLoadLexicon$

go: go.mod file not found in current directory or any parent directory; go:在当前目录或任何父目录中找不到 go.mod 文件; see 'go help modules'见“去帮助模块”

Test run finished at 29/08/2022, 02:58:53 < `测试运行于 29/08/2022 02:58:53 完成 < `

You need to add a go.mod file to your project's root directory.您需要将go.mod文件添加到项目的根目录。

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

Example:例子:

go mod init project-name


go mod init example.com/project-name


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

You may need to use the tidy command to clean up after running one of the above commands.运行上述命令之一后,您可能需要使用 tidy 命令进行清理。

go mod tidy

Use the path format from above when importing a package into your go file将 package 导入 go 文件时,使用上述路径格式

Example:例子:

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"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Is go.mod in Go the same thing as package.json in JavaScript (node)? - Is go.mod in Go the same thing as package.json in JavaScript (node)? 如何修复 $GOPATH/go.mod 存在但不应该存在 - Linux Fedora - How do I fix $GOPATH/go.mod exists but should not - Linux Fedora “所选目录不是 Go Sdk 的有效主目录” - "The selected directory is not a valid home for Go Sdk" Go 构建失败:使用 go mod 文件运行“构建”时“下载不一致的图块” - Go build failure: "Downloaded inconsistent tile" when running "go build" with go mod file Firebase:此文件不存在且没有索引。在当前目录中找到html或在根目录中找到404.html - Firebase: This file does not exist and there was no index.html found in the current directory or 404.html in the root directory GCS 数据流抛出错误 - FileNotFoundException(找不到此类文件或目录) - GCS Dataflow throws an error - FileNotFoundException(No such file or directory found) 如何将 function 从另一个目录连接到 Go/Echo? - How do I connect a function from another directory to Go/Echo? Go Mod 在Raspberry Pi4上下载 - Go Mod Download on Raspberry Pi4 Go Mod 需要什么意思 - What does the Go Mod require mean 错误“protoc-gen-go:程序未找到或不可执行” - Error "protoc-gen-go: program not found or is not executable"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM