build – 子目录中的Golang测试

我想在Go中创建一个包作为子目录的测试和示例,以保持工作空间更清洁。这是可能的,如果是如此?

所有的文档总是把测试代码放在同一个地方作为其他代码,这是更好的在某些方面或只是约定?

谢谢。

注意,你可以运行go test“recursively”:你需要列出你想测试的所有包。

如果您是Go项目的根文件夹,请键入:

go test ./...

‘。/ …’符号在“command go”的“Description of package lists”部分中描述:

An import path is a pattern if it includes one or more “...” wildcards,each of which can match any string,including the empty string and strings containing slashes.

Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.

As a special case,x/... matches x as well as x‘s subdirectories.
For example,net/... expands to net and packages in its subdirectories.

如果你将_test.go文件保存在一个文件夹中,’go test。/ …’命令将能够接受它们。
但:

>您将需要使用包的名称为导出的变量和函数(在测试中使用)添加前缀,以便测试文件能够访问包导出的内容
>您不会访问非导出的内容

话虽如此,我仍然更喜欢将_test.go文件保存在主源文件的旁边:它更容易找到。

相关文章

什么是Go的接口? 接口可以说是一种类型,可以粗略的理解为他...
1、Golang指针 在介绍Golang指针隐式间接引用前,先简单说下...
1、概述 1.1 Protocol buffers定义 Protocol buffe...
判断文件是否存在,需要用到"os"包中的两个函数: os.Stat(...
1、编译环境 OS :Loongnix-Server Linux release 8.3 CPU指...
1、概述 Golang是一种强类型语言,虽然在代码中经常看到i:=1...