qinking126
qinking126

Reputation: 11915

go run: cannot run non-main package

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

Upvotes: 110

Views: 141008

Answers (5)

Kanchan Sarkar
Kanchan Sarkar

Reputation: 463

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".

Upvotes: 4

All Іѕ Vаиітy
All Іѕ Vаиітy

Reputation: 26482

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

Upvotes: 8

Pablo C. García
Pablo C. García

Reputation: 22404

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

package Main

instead of

package main

Upvotes: 39

tux21b
tux21b

Reputation: 94799

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.

Upvotes: 62

Sam P
Sam P

Reputation: 1841

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).

Upvotes: 26

Related Questions