我想让一个特定的文件出现在编辑器文件列表的顶部,所以我给它加上了_前缀。它看起来是这样的:
mypkg
_func.go
a.go
b.go我知道Go使用_test,_unix等的文件命名约定,但是,既然_func不匹配特定的架构或者是一个测试用例,为什么它不能算作源文件?
当我导入此包时,此文件中定义的函数不可用。
发布于 2013-02-12 23:08:29
显然,下划线的权重与文件开头的点前缀相同,go build命令显然忽略了它。然而,这不是go工具的决定,而是标准库中的go/build包的决定。您可以看到负责的行here。
我的猜测是临时文件以下划线为前缀,这样构建工具链就会忽略它们。
编辑:This comment记录了行为。我引述如下:
// Import returns details about the Go package named by the import path,
// interpreting local import paths relative to the srcDir directory.
// If the path is a local import path naming a package that can be imported
// using a standard import path, the returned package will set p.ImportPath
// to that path.
//
// In the directory containing the package, .go, .c, .h, and .s files are
// considered part of the package except for:
//
// - .go files in package documentation
// - files starting with _ or . (likely editor temporary files)
// - files with build constraints not satisfied by the context
//
// If an error occurs, Import returns a non-nil error and a non-nil
// *Package containing partial information.
//您可以在package docs of package go/build中以用户友好的形式找到它。
https://stackoverflow.com/questions/14834973
复制相似问题