我正在開發一個創建附加結構的專案。
在tests->gchat_test我有兩個檔案init.go和gchat_test.go
//init.go
package gchat_test
//initiation code
//gchat_test.go
package gchat_test
//testing code
對于這些檔案,我在 VSCode 中遇到以下錯誤
found packages gchat (gchat_test.go) and gchat_test (init.go) in /home/<<>>/errornotifier/tests/gchat_test
我也從控制臺嘗試過,只是為了確保這不是 VSCode 問題
~/Loans/errornotifier/tests/gchat (feture/initial)$ go test
found packages gchat (gchat_test.go) and gchat_test (init.go) in /home/<<>>/errornotifier/tests/gchat_test
我對另一組測驗有類似的結構,但我沒有遇到這個錯誤
在tests->kafka_test我有兩個檔案init.go和kafka_test.go
//init.go
package kafka_test
//initiation code
//kafka_test.go
package kafka_test
//testing code
作為一個快速修復,我將啟動移動tests->gchat_test->init.go到tests->gchat_test->gchat_test.go并洗掉,init.go然后它就可以作業了。
我想了解為什么會引發此錯誤,即使這些檔案具有相同的包名稱以及如何修復它

uj5u.com熱心網友回復:
沒有_test.go后綴的檔案將構成包的源檔案。帶有_test.go后綴的檔案被排除在外go build。宣告帶有_test后綴的包的測驗檔案形成不同的包。
所以基本上你有一個由非測驗檔案定義的普通包,你有一個由測驗檔案定義的測驗包。這是否可行取決于 Go 工具的實作。gchat_testinit.gogchat_testgchat_test.go
總而言之,_test不應該在非測驗包中使用包名。 init.go是一個非測驗檔案,它不應該宣告一個測驗包。
編譯包和依賴:
編譯包時,build 會忽略以 '_test.go' 結尾的檔案。
測驗包:
'Go test' 重新編譯每個包以及名稱與檔案模式“*_test.go”匹配的任何檔案。這些附加檔案可以包含測驗函式、基準函式、模糊測驗和示例函式。有關更多資訊,請參見“go help testfunc”。每個列出的包都會導致執行單獨的測驗二進制檔案。名稱以“_”(包括“_test.go”)或“.”開頭的檔案 被忽略。
宣告帶有后綴“_test”的包的測驗檔案將被編譯為單獨的包,然后與主測驗二進制檔案鏈接并運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470378.html
