gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:11:48 27 4
gpt4 key购买 nike

我希望一个特定的文件出现在我的编辑器文件列表的顶部,所以我在它前面加上了 _ 前缀。这是它的样子:

mypkg
_func.go
a.go
b.go

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

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

最佳答案

显然,下划线的权重与文件开头的点前缀相同,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 中以用户友好的形式找到它.

关于以下划线字符开头的 Go 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14834973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com