首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以下划线字符开头的Go文件名

以下划线字符开头的Go文件名
EN

Stack Overflow用户
提问于 2013-02-12 22:37:52
回答 2查看 3.6K关注 0票数 9

我想让一个特定的文件出现在编辑器文件列表的顶部,所以我给它加上了_前缀。它看起来是这样的:

代码语言:javascript
复制
mypkg
  _func.go
  a.go
  b.go

我知道Go使用_test_unix等的文件命名约定,但是,既然_func不匹配特定的架构或者是一个测试用例,为什么它不能算作源文件?

当我导入此包时,此文件中定义的函数不可用。

EN

Stack Overflow用户

回答已采纳

发布于 2013-02-12 23:08:29

显然,下划线的权重与文件开头的点前缀相同,go build命令显然忽略了它。然而,这不是go工具的决定,而是标准库中的go/build包的决定。您可以看到负责的行here

我的猜测是临时文件以下划线为前缀,这样构建工具链就会忽略它们。

编辑:This comment记录了行为。我引述如下:

代码语言:javascript
复制
// 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中以用户友好的形式找到它。

票数 14
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14834973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档