go test遇到的同一包下的变量报错undefined

使用go test array_test.go命令执行测试文件时,报错显示调用的同一包下array.go文件中的方法undefined。
在这里插入图片描述
原因是go test会为指定的源码文件生成一个虚拟代码包——“command-line-arguments”,而array_test.go引用了其他包中的数据并不属于代码包“command-line-arguments”,编译不通过,错误自然发生了。

因此,我们可以在go test的时候加上引用的包。

//执行整个测试文件
go test -v array_test.go array.go
//执行测试文件中的指定方法
go test -v array_test.go array.go -test.run TestNewArray
发布了19 篇原创文章 · 获赞 1 · 访问量 224

猜你喜欢

转载自blog.csdn.net/helen920318/article/details/105017118