英文:
How do you structure imports for testing inside a module of your project?
问题
我正在尝试理解如何将一个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
无法导入"tool"包,你可能可以输入完整路径,但是当我在GitHub上查看时,没有人在他们的模块中这样做。所以我不明白我做错了什么。
英文:
I am attempting to understand how to structure a go project into sub modules stored in separate source code repositories (on host example.com
), however when I do, I am not sure how to run the tests that are within a module. What am doing wrong in the following example, any help is much appreciated!!
<!-- language: lang-bash -->
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
When I run this test, this is the error I am seeing:
# 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]
Obviously book_test.go
can't import the "tool" package, an you could probably put in the full path, but when I look in github, no one does that in their modules. So I don't understand what I am doing wrong.
答案1
得分: 2
你的代码中的这一行
import "tool"
是有问题的。标准库中没有名为tool的包,而你的tool包的导入路径是example.com/john/tool
。
只需删除这个导入语句即可。没有必要导入当前包(而且这是不可能的,因为这将导致(退化的)导入循环)。
英文:
Your line
import "tool"
is the offender. There is no package tool in the standard library and your package tool has an import path of example.com/john/tool
.
Just drop that import. There is no need to import the current package (and it is impossible as this would be a (degenerated) import cycle).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论