Golang: “package ast_test” 下划线测试

huangapple go评论95阅读模式
英文:

Golang: "package ast_test" underscore test

问题

在Golang的stdlib中,源文件的基本目录是ast

在该文件中指定的包是ast_test吗?

在同一目录中的所有其他文件中指定的包是ast

根据golang.org的说明:
src目录包含按包组织的Go源文件(每个目录一个包)...
按照惯例,包名使用小写的单词,不需要使用下划线或混合大小写。
...另一个惯例是包名与其源目录的基本名称相同。

如何在一个文件夹中有多个包(这里有2个)是可能的吗?

英文:

Source file from Golang's stdlib

File's base directory: ast

Package specified in the file: ast_test ???

Package specified in all other files inside the same directory: ast

From golang.org:
src contains Go source files organized into packages (one package per directory) ...
By convention, packages are given lower case, single-word names; there should be no need for underscores or mixedCaps
... Another convention is that the package name is the base name of its source directory

How is it possible to have multiple packages (here 2) in one folder?

答案1

得分: 7

你可以在src/pkg/go/ast/commentmap_test.go中找到另一个例子,其中有以下注释:

// 为了避免与go/parser产生循环依赖,这个文件放在了一个单独的包中。

我猜它允许执行类似于以下命令:

go test

这将测试解析器功能,同时避免将该测试作为相同解析器功能的一部分(因为它已经放在了一个单独的包中)。

根据go命令手册

> 声明一个带有后缀“_test”的包的测试文件将被编译为一个单独的包,然后与主测试二进制文件链接并运行。


这个帖子提出了以下问题:

> > 现在go工具要求每个目录都是一个包,并且不允许在同一个文件夹中有不同包名的文件,那么package关键字有什么用处呢?它似乎是一个不必要的重复。

> > 编译器是否需要它,或者是否有计划将其删除?

答案暗示了你可以在一个文件夹中有多个包:

> 包声明声明了包的名称。
Go语言不知道文件或目录是什么,导入路径本身不会影响被导入的包的实际名称。所以编译器知道如何称呼这个包的唯一方式就是通过包声明。

> 语言并不要求不同的包必须放在不同的目录中;这是go工具的要求。
另一种假设的实现可能没有这个要求。

> 即使这个go工具的要求可以通过“// +build”构建标签绕过。

例如,阅读misc/cgo/gmpmisc/cgo/stdio(一些文件包括// +build ignore

英文:

You find another example in src/pkg/go/ast/commentmap_test.go, with the comment:

// To avoid a cyclic dependency with go/parser, this file is in a separate package.

I suppose it allows for an othogonal command like:

go test

That will test parser features while avoiding for that test to be part of the same parser features (since it has been put in a separate package)

From go command man page:

> Test files that declare a package with the suffix "_test" will be compiled as a separate package, and then linked and run with the main test binary.


This thread asked the question:

> > Now that the go tool requires each directory to be one package and doesn't allow to have files with different package names inside the same folder, how is the package keyword useful? It seems like a unnecessary repetition.

> > Is it required by the compiler or is there any plan to remove it?

The answers hinted at the fact that you can have more than one package in a folder:

> The package declaration declares the name of the package.
The language Go doesn't know what a file or a directory is and the import path itself doesn't effect the actual name of the package that is being imported. So the only way the compiler knows what to call the package is the package declaration.

> The language doesn't require separate packages to be in separate directories; it is a requirement of the go tool.
Another hypothetical implementation may not have this requirement.

> Even this go tool requirement can be bypassed thanks to the "// +build" build tags.

For example, read misc/cgo/gmp or misc/cgo/stdio (some files include // +build ignore)

huangapple
  • 本文由 发表于 2014年7月14日 15:16:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/24731370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定