【问题标题】:Why the functions defined in other "main" packages are not recognised?为什么无法识别其他“主要”包中定义的功能?
【发布时间】:2015-04-18 17:42:21
【问题描述】:

我必须归档 main.go 和 main2.go 。在 main.go 中,我定义了 main() 函数以及 main2.go 中的调用 somefunc()。问题是当我运行 go run main.go 时,它说 somefunc() 是未定义的。基本上它不会扫描包中的其他主要功能。但是,如果我在 main.go 中声明这个 somefunc() 它可以工作,但是当我运行 go test 时,它说该函数被重新声明。

问题:有什么方法可以告诉go run 表现得像 go test 并编译/运行包中的所有文件(在本例中为 main.go 和 main1.go),而不仅仅是 main.go ?

【问题讨论】:

标签: go


【解决方案1】:

您必须包含所有文件作为go run 的参数。

go run main1.go main.go

go *.go

除非同一文件夹中有测试文件。

【讨论】:

  • 虽然go build && ./progname更可取,但如果有测试文件和/或具有构建约束的文件并且您真的想使用@ 987654325@ 您可以这样做(或为其创建别名):go run `go list -f '{{join .GoFiles " "}}'` (假设文件名中没有空格或“奇怪”字符)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
相关资源
最近更新 更多