0
我在嘗試瞭解如何將存儲在不同源代碼存儲庫(主機example.com
)中的子模塊構建爲go項目,但是當我這樣做時,我不確定如何運行模塊中的測試。在下面的例子中,我做了什麼錯誤,非常感謝任何幫助!您如何在您的項目模塊中構建導入以進行測試?
mkdir -p src/example.com/john/tool
echo "package tool" >> src/example.com/john/tool/book.go
echo "" >> src/example.com/john/tool/book.go
echo "type Book struct {" >> src/example.com/john/tool/book.go
echo " Title string" >> src/example.com/john/tool/book.go
echo "}" >> src/example.com/john/tool/book.go
echo "" >> src/example.com/john/tool/book.go
echo "package tool" >> src/example.com/john/tool/book_test.go
echo "" >> src/example.com/john/tool/book_test.go
echo "import (" >> src/example.com/john/tool/book_test.go
echo " \"tool\"" >> src/example.com/john/tool/book_test.go
echo " \"testing\"" >> src/example.com/john/tool/book_test.go
echo ")" >> src/example.com/john/tool/book_test.go
echo "" >> src/example.com/john/tool/book_test.go
echo "func TestBook(t *testing.T) { }" >> src/example.com/john/tool/book_test.go
echo "" >> src/example.com/john/tool/book_test.go
export GOPATH=`pwd`
go test example.com/john/tool
當我運行這個測試,這是我看到的錯誤:
# example.com/john/tool
src/example.com/john/tool/book_test.go:4:3: cannot find package "tool" in any of:
/usr/local/go/src/tool (from $GOROOT)
/Users/john/app/src/tool (from $GOPATH)
FAIL example.com/john/tool [setup failed]
顯然book_test.go
不能進口的「工具」包中,你很可能把完整路徑,但是當我看着github時,沒有人在模塊中做這件事。所以我不明白我做錯了什麼。
爲什麼要從_test.go文件再次導入軟件包「工具」?這是不需要的,因爲它正在尋找其他「工具」包,所以造成了這個問題。 book_test.go已經是您正在創建的工具包的一部分。 –