Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 643
qinking126 Avatar asked May 26 '14 13:05

qinking126


People also ask

Is package main required in Go?

The first statement in a Go source file must be package name . Executable commands must always use package main .

How do I run Go Main?

To run a go program, create a file with an extension .go and write your golang code in that file. For example, we will create a file named helloworld.go and write the following code in it. Now open command prompt and navigate to the location of helloworld.go file.

How do I run a package in Go?

Add the Go install directory to your system's shell path. That way, you'll be able to run your program's executable without specifying where the executable is. Once you've updated the shell path, run the go install command to compile and install the package. Run your application by simply typing its name.

What is Golang package Main?

Package Main The package “main” tells the Go compiler that the package should compile as an executable program instead of a shared library. The main function in the package “main” will be the entry point of our executable program.


1 Answers

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.

like image 167
tux21b Avatar answered Sep 27 '22 21:09

tux21b