go run: 无法运行非主包

huangapple go评论90阅读模式
英文:

go run: cannot run non-main package

问题

这是一个简单的Go应用程序。如果我运行以下代码,会出现"go run: cannot run non-main package"错误。

package zsdfsdf

import (
	"fmt"
)

func Main() {
	fmt.Println("sddddddd")
}

要修复这个错误,我只需要将包的名称改为main。但是我不明白为什么我需要这样做。我应该可以随意命名包。

另一个问题是,我知道main函数是程序的入口点,你需要它,否则程序将无法工作。但是我看到一些代码没有main函数,仍然可以正常工作。

点击这个链接,页面底部的示例没有使用main包和main函数,但仍然可以正常工作。只是好奇为什么。

https://developers.google.com/appengine/docs/go/gettingstarted/usingdatastore

英文:

here the simple go application. I am getting "go run: cannot run non-main package" error, if I run following code.

package zsdfsdf

import (
	"fmt"
)

func Main() {
	fmt.Println("sddddddd")
}

To fix it, I just need to name the package to main. But I don't understand why I need to do that. I should be able to name the package whatever I want.

Another question, I know main function is the entry point of the program, you need it. otherwise it will not work. But I see some codes that didn't have main function still works.

Click on this link, the example at the bottom of the page didn't use package main and main function, and it still works. just curious why.

https://developers.google.com/appengine/docs/go/gettingstarted/usingdatastore

答案1

得分: 53

每个Go程序的入口点是main.main,即一个名为main的包中的一个名为main的函数。你必须提供这样一个main包。

但是,GAE是一个例外。它们会自动为你的项目添加一个名为main的包,并包含main函数。因此,你不被允许编写自己的main包。

英文:

The entry point of each go program is main.main, i.e. a function called main in a package called main. You have to provide such a main package.

GAE is an exception though. They add a main package, containing the main function automatically to your project. Therefore, you are not allowed to write your own.

答案2

得分: 36

你需要使用main包,在Go语言中,常见的错误是将代码中的package Main写成package main

英文:

You need to use the main package, a common error starting with go is to type

package Main

instead of

package main

答案3

得分: 24

你需要在你的app.yaml文件中指定你的应用程序访问点。请参考这里。你需要指定:

application: zsdfsdf

还可以从上面的链接中看到:
> "注意:当编写独立的Go程序时,我们会将这段代码放在main包中。Go App Engine Runtime提供了一个特殊的main包,所以你应该将HTTP处理程序代码放在你选择的包中(在这种情况下是hello包)。"

你是正确的,所有的Go程序都需要Main方法。但是这个方法是由Google App Engine提供的。这就是为什么你提供的示例可以工作。你的示例在本地(非GAE环境)上无法工作。

英文:

You need to specify in your app.yaml file what your app access point is. Take a look here. You need to specify:

application: zsdfsdf

Also see from that above link:
> "Note: When writing a stand-alone Go program we would place this code
> in package main. The Go App Engine Runtime provides a special main
> package, so you should put HTTP handler code in a package of your
> choice (in this case, hello)."

You are correct that all Go programs need the Main method. But it is provided by Google App Engine. That is why your provided example works. Your example would not work locally (not on GAE).

答案4

得分: 8

避免这个错误的解决方案是将入口点somefilename.go文件定义为主包,方法是在入口点的第一行添加package main

package main

// 导入语句
import "fmt"

// 以下是代码
英文:

A Solution to avoid this error is defining entry point somefilename.go file as main package by adding package main as the first line of the entry point

package main

// import statements 
import "fmt"

// code below

答案5

得分: 4

为了避免这个问题,你可以按照以下方式修改代码:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("sddddddd")
}

将包名重命名为"main",将函数名重命名为"main",而不是"Main"。

英文:

To avoid the problem you can modify the code as follow

package main

import (
    "fmt"
 )

func main() {
    fmt.Println("sddddddd")
 }

rename the package as "main" and rename the function as "main" instead of "Main".

huangapple
  • 本文由 发表于 2014年5月26日 21:02:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/23870801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定