英文:
Go test file for specific OS or architecture
问题
在Go语言中,文件名具有语义意义。例如:
*_windows.go // 仅在Windows编译时包含
*_unix.go // 仅在Unix编译时包含
*_386.go // 仅在386系统编译时包含
*_test.go // 仅在运行`go test`时包含
然而,我似乎无法使以下内容起作用:
*_windows_test.go // 仅在Windows上运行`go test`时包含
*_test_windows.go // 另一种尝试
在Go语言中是否可能实现这个功能?如果是,该如何实现?
英文:
In Go, filenames have semantic meaning. For example:
*_windows.go // Only include in compilations for Windows
*_unix.go // Only include in compilations for Unix
*_386.go // Only include in compilations for 386 systems
*_test.go // Only include when run with `go test`
However, I can't seem to get the following to work:
*_windows_test.go // Only include when running `go test` on windows
*_test_windows.go // Another attempt
Is this even possible with Go? If so, how?
答案1
得分: 4
只需在测试文件上使用构建约束。
// +build windows
构建约束是由空格分隔的选项的逻辑或运算;每个选项由逗号分隔的项的逻辑与运算组成;每个项是一个字母数字单词,或者在前面加上!表示其否定。也就是说,构建约束的格式为:
英文:
Just use a build constraint on the test files.
// +build windows
> A build constraint is evaluated as the OR of space-separated options; each option evaluates as the AND of its comma-separated terms; and each term is an alphanumeric word or, preceded by !, its negation. That is, the build constraint:
答案2
得分: 3
原来我错了。它确实有效。问题在于我还有一个名为 foo_unix_test.go
的文件,显然 Go 不支持 *_unix.go
语法作为特殊情况。
英文:
Turns out I was wrong. It does work. The issue was that I also had a file called foo_unix_test.go
, and apparently Go doesn't support the *_unix.go
syntax as a special case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论