英文:
Go file not running which is not in main package
问题
我有一个非常简单的Go项目设置。
在根目录下,我有一个go.mod
文件,一个main.go
文件和一个名为main2
的文件夹。在main2
文件夹内有一个main2.go
文件。
/
|_ go.mod
|_ main.go
|_ main2
|_ main2.go
从根目录下,我尝试运行go run
命令
go run main2/main2.go
但是它报错了:
package command-line-arguments is not a main package
有人可以帮忙吗?
英文:
I have a very simple Go project setup.
At root directory I have go.mod
file and main.go
and a folder called main2
. Inside main2
folder there is main2.go
file.
/
|_ go.mod
|_ main.go
|_ main2
|_ main2.go
From root directory I am trying to run go run command
go run main2/main2.go
and it throws error:
> package command-line-arguments is not a main package
Can someone help?
答案1
得分: 9
你的main2.go文件的包名必须是main。当存在一个名为main的包和一个名为main的函数时,编译器会知道它将被编译为可执行文件,而不是库文件。
所以请尝试将main2/main2.go文件中的package command-line-arguments改为package main。
英文:
The package of your main2.go file must be main. When there is a main package, and a function main in your project, the compiler knows it will be compiled as a executable, and not as a library.
So try to change package command-line-arguments to package main inside the main2/main2.go file.
答案2
得分: 6
想象一个用golang编写的可执行文件,就像是一个只有前门和许多不同房间的房子。一旦你进入房子,你可以通过任何门进入,但是要进入房子,你必须先通过前门。这个前门就是main()函数。
在Golang中,可执行文件的入口是通过main()函数进入的。如果你想为一个单独的可执行文件运行不同的逻辑路径,你可以使用main()作为路由函数,通过命令行参数将控制权传递给其他包:
package main
import (
"os"
"otherpackage"
// 在这里导入你的子包。
)
func main() {
// 第一个参数
// 总是程序名称
// 所以os.Args[1]是第一个动态参数
arg1 := os.Args[1]
// 使用arg1来决定调用哪些包
if arg1 == "option1" {
// 在这里执行option1的代码。
otherpackage.DoThis()
}
if arg1 == "option2" {
// 在这里执行option2的代码。
otherpackage.DoThat()
}
}
然后你可以像这样运行你的程序:
go run main.go option1
程序执行
通过将一个名为main的单个未导入包与它导入的所有包进行链接,可以创建一个完整的程序。主包必须具有包名main,并声明一个不带参数且不返回值的main函数。
英文:
Imagine a golang executable as a house with only a front door and many different rooms inside it. You can go through any door you want once you are in the house, but to get inside you have to go through the front door first. That front door is the main() function.
Golang's entry point into an executable is through main(). If you want to run different logic paths for a single executable, you can use main() as a routing function to the other packages using command line arguments:
package main
import (
"os"
"otherpackage"
// Your child packages get imported here.
)
func main() {
// The first argument
// is always program name
// So os.Args[1] is the first dynamic argument
arg1 := os.Args[1]
// use arg1 to decide which packages to call
if arg1 == "option1" {
// option1 code executes here.
otherpackage.DoThis()
}
if arg1 == "option2" {
// option2 code executes here.
otherpackage.DoThat()
}
}
Then you can run your program with something like:
go run main.go option1
From golang documentation:
> Program execution
A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论