亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤消息: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 關注
  • 345 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號