go run: cannot run non-main package

11,837 views
Skip to first unread message

john...@gmail.com

unread,
May 26, 2014, 9:04:01 AM5/26/14
to golan...@googlegroups.com

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 dont 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

Dan Kortschak

unread,
May 26, 2014, 9:12:12 AM5/26/14
to john...@gmail.com, golan...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Mercl

unread,
May 26, 2014, 9:13:06 AM5/26/14
to john...@gmail.com, golang-nuts
On Mon, May 26, 2014 at 3:04 PM, <john...@gmail.com> wrote:
> 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 dont understand why
> I need to do that. I should be able to name the package whatever I want.

No, you are not permitted to name the package whatever you want
because the specification says otherwise:

""""
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.
""""[0]

> 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

There are packages. Some packages are programs/command (package main
with func main). The other packages are not programs but libraries
eventually imported by some program/command (package main with func
main.)

See also [1], [2] and [3] (in no particular order).

[0]: http://golang.org/ref/spec#Program_execution
[1]: http://golang.org/doc/code.html#Command
[2]: http://golang.org/doc/code.html#Library
[3]: http://golang.org/ref/spec

-j

john...@gmail.com

unread,
May 26, 2014, 9:45:44 AM5/26/14
to golan...@googlegroups.com
Jan & Dan, thank you so much.  I didnt know google provide main package and main function automatically.

Jan Mercl

unread,
May 26, 2014, 9:55:49 AM5/26/14
to john.qin, golang-nuts
On Mon, May 26, 2014 at 3:45 PM, <john...@gmail.com> wrote:
> I didnt know google provide main package and
> main function automatically.

Go does not provide an automatic main package[0].

I'm sorry if I may have written something implying that - even if I'm
not aware of it.

[0]: In the first approximation. 'go test' does it but that's not
related to this discussion.

-j

chris dollin

unread,
May 26, 2014, 10:10:18 AM5/26/14
to Jan Mercl, john.qin, golang-nuts
On 26 May 2014 14:54, Jan Mercl <0xj...@gmail.com> wrote:
On Mon, May 26, 2014 at 3:45 PM,  <john...@gmail.com> wrote:
> I didnt know google provide main package and
> main function automatically.

Go does not provide an automatic main package[0].

I'm sorry if I may have written something implying that - even if I'm
not aware of it.

I think by "google" he meant "google app engine".
 
Chris

--
Chris "allusive" Dollin

Dan Kortschak

unread,
May 26, 2014, 4:06:54 PM5/26/14
to Jan Mercl, john.qin, golang-nuts
This is one of those rare cases when it was correct to use Google instead of Go. The OP was referring to the absence of a main.main in an example for appengine datastore.
Reply all
Reply to author
Forward
0 new messages