go 文档:澄清特殊目录仍可用于包路径

jljoyd4f  于 2个月前  发布在  Go
关注(0)|答案(7)|浏览(31)

你好,

当前的文档在涉及到go工具理解的特殊目录方面有些不幸。
https://golang.org/cmd/go/#hdr-Test_packages
go工具将忽略一个名为"testdata"的目录,使其可用于存放测试所需的辅助数据。
https://golang.org/cmd/go/#hdr-Package_lists
以"."或"_"开头的目录和文件名以及名为"testdata"的目录都被go工具忽略。
然而,这些目录目前也可以很好地用于编译。不幸的是,我还没有找到一个更好的措辞来描述这些特殊规则/目录下只有某些操作会在这种特殊模式下表现。
这种情况经常出现,因为工具可以建议从这些目录中获取完成符号,然后用户可能会将其解释为工具问题,而不是某些特定go工具使用中的特例。
谢谢。

pvcm50d1

pvcm50d11#

will not build code that is under testdata directories
Under testdata directories or under directories whose suffix is testdata ? AFAIK is the 2nd
What it means is that commands like go test ./... will not build code ...........
If my 1st assumption is correct (directories that have as suffix testdata ) then go test ./... is building for me those directories

It is correct to place .go code under testdata folders? e.g. fixtures. If so, the behaviour I would expect is go build command to ignore these folders.

lbsnaicq

lbsnaicq2#

只有名字中包含 testdata 的目录会被忽略。名字带有 testdata 后缀的目录没有什么特殊之处。

将 Go 文件放在一个 testdata 目录中是可以的。你可以在标准库中看到很多例子。行为如我所说:go test ./... 会忽略 testdata 目录下的 Go 文件。但是,如果你,例如,进入 cd 目录并运行 go build ,go 工具会像往常一样构建该目录。它不会在文件树中查找是否在 testdata 目录下。

5n0oy7gb

5n0oy7gb3#

对不起,我不确定你在建议什么。当你说“那些目录目前也可以很好地用于编译?”时,你是什么意思?无论它意味着什么,是否足够重要以便在文档中被提及?谢谢。

kgqe7b3p

kgqe7b3p4#

我认为目录'testdata'(及其子目录)应该只包含测试夹具。从文档中我了解到,放在'testdata'中的所有内容在编译过程中都会被忽略/未定义。
如果我将代码放入一个名为'.code.go'的文件中,并尝试引用它,会导致编译错误(未定义)。然而,我可以从名为'.pkg'的目录中导入代码。这种行为至少是不一致的。
我认为以下情况:
以"."或"_"开头的目录和文件名
应该与'.code.go'的行为相同

68de4m5k

68de4m5k5#

我同意@russoj88的观点。有人能解释一下testdatago tool忽略的真正含义吗?这会产生什么影响?

tyky79it

tyky79it6#

我希望testdata只包含固定装置,你能澄清一下吗?@ianlancetaylor

x4shl7ld

x4shl7ld7#

这意味着像go test ./...这样的命令不会构建位于testdata目录下的代码。同样,go工具会忽略以"."或"_"开头的目录。
go工具不会阻止你以各种方式显式引用"testdata"。

相关问题