我正在嘗試使用 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"
)
- 1 回答
- 0 關注
- 345 瀏覽
添加回答
舉報
0/150
提交
取消
