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
The first statement in a Go source file must be package name . Executable commands must always use package 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With