在官方文档中哪里解释了 Go 中变量的范围?

Where is explained the scope of variables in Go in official documentation?

我在 go.dev 中找不到任何解释 private/global 变量和变量范围的地方。

作为一个相关问题,我在尝试从 _test.go 文件中导入变量时遇到了困难。当然这不在那份文档中,但我认为与编译器有关?

声明和范围在 Language Specification, for details and comprehensive overview, see 的不同部分进行了详细说明。

正如您在评论中提到的,您的问题是您无法从 _test.go 文件中导入变量。语言规范没有提到这一点,所以这是一个实现限制。

引用自Command documentation: Compile packages and dependencies

When compiling packages, build ignores files that end in '_test.go'.

它们仅在您 运行 测试时使用。 Test packages:

'Go test' recompiles each package along with any files with names matching the file pattern "*_test.go". These additional files can contain test functions, benchmark functions, and example functions. See 'go help testfunc' for more. Each listed package causes the execution of a separate test binary. Files whose names begin with "_" (including "_test.go") or "." are ignored.

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.