我试图了解如何将go项目构建为存储在单独的源代码存储库(在主机example.com
上)的子模块,但是当我这样做时,我不确定如何运行其中的测试一个模块。在以下示例中出现了什么问题,非常感谢任何帮助!!
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时,没有人在他们的模块中这样做。所以我不明白我做错了什么。
答案 0 :(得分:2)
你的行
import "tool"
是罪犯。标准库中没有包工具,您的包工具的导入路径为example.com/john/tool
。
放弃导入。没有必要导入当前包(并且这是不可能的,因为这将是(退化的)导入周期。)