为什么要在其他“主要"中定义功能?包裹不被认可? [英] Why the functions defined in other "main" packages are not recognised?

查看:50
本文介绍了为什么要在其他“主要"中定义功能?包裹不被认可?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to files main.go and main2.go . In main.go I have the main() function defined along with a call somefunc() which is in main2.go. The issue is that when I run go run main.go it says that somefunc() is undefined. Basically it doesn't scan the other main functions from package. However if I declare this somefunc() in main.go it works but when I run go test it says the function is redeclared.

问题:我有什么办法可以告诉 go run 像go test一样运行并编译/运行包中的所有文件(在这种情况下,main.go和main1.go)不只是main.go吗?

Question: Is there any way that I can tell to go run to behave like go test and compile/run all the files from the package(in this case both main.go and main1.go) not just main.go?

推荐答案

您必须将所有文件作为 go run 的参数.

You must include all the files as argument of the go run.

go run main1.go main.go

go *.go

除非同一文件夹中有测试文件,否则.

Unless there are test files in the same folder.

这篇关于为什么要在其他“主要"中定义功能?包裹不被认可?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆