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

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

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

问题

我正在尝试使用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)
		}
	}
}

我得到了这个错误:

Running tool: /usr/local/go/bin/go test -timeout 30s -run ^TestLoadLexicon$

go: go.mod file not found in current directory or any parent directory; see 'go help modules'


> Test run finished at 29/08/2022, 02:58:53 <
英文:

I am trying to run a unit test using go. The functions work properly in the main file. The function is given below:

func LoadLexicon(lexiconPath string) (map[string]string, error) {
	m := make(map[string]string)
	lexiconPath = strings.TrimSuffix(lexiconPath, &quot;\n&quot;)
	if lexiconPath == &quot;nil&quot; {
		m[&quot;COME&quot;] = &quot;k V m&quot;
		m[&quot;WORDS&quot;] = &quot;w 3` d z&quot;
		m[&quot;MECCA&quot;] = &quot;m E k @&quot;

		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, &quot;\t&quot;)
		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
	}{
		{&quot;COME&quot;, &quot;k V m&quot;},
		{&quot;WORDS&quot;, &quot;w 3` d z&quot;},
		{&quot;MECCA&quot;, &quot;m E k @&quot;},
	}
	for _, tc := range tests {
		if got, _ := LoadLexicon(&quot;nil&quot;); got[tc.n] != tc.want {
			t.Errorf(&quot;got %s, want %s&quot;, got[tc.n], tc.want)
		}
	}
}

I am getting this error
`
Running tool: /usr/local/go/bin/go test -timeout 30s -run ^TestLoadLexicon$

go: go.mod file not found in current directory or any parent directory; see 'go help modules'

> Test run finished at 29/08/2022, 02:58:53 <
`

答案1

得分: 2

你需要在项目的根目录中添加一个 go.mod 文件来管理依赖项。

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

示例:

go mod init 项目名称


go mod init example.com/项目名称


go mod init github.com/你的用户名/项目名称

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

go mod tidy

在导入包到你的 Go 文件时,请使用上述路径格式。

示例:

import (
   // 这样导入内部和外部包
   "github.com/你的用户名/项目名称/包名"

   // 正常方式导入标准库包
   "testing"
   "math/rand"
)
英文:

You need to add a go.mod file to your project's root directory.

Use modules to manage dependencies. Official docs: 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.

go mod tidy

Use the path format from above when importing a package into your go file

Example:

import (
   // Import internal and external packages like this
   &quot;github.com/you-user-name/project-name/package-name&quot;

   // Import standard library packages the normal way
   &quot;testing&quot;
   &quot;math/rand&quot;
)

huangapple
  • 本文由 发表于 2022年8月29日 05:07:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/73522277.html
匿名

发表评论

匿名网友

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

确定