Is the package name must same with name folder name?

5,900 views
Skip to first unread message

Nan Xiao

unread,
Aug 8, 2015, 3:44:19 AM8/8/15
to golang-nuts
Hi all,

I read the following sentence in one Go book:  

"All code files in a folder must use the same package name, and it’s common practice
to name the package after the folder."

Per my understanding, "common practice" means the name of package can differ from 
the folder. But after testing in Windows, I find the package name must comply with folder
name, else compile error occurs.

Is the package name must same with name folder name? Or my test is wrong.

Thanks in advance!

Best Regards
Nan Xiao

Tamás Gulácsi

unread,
Aug 8, 2015, 4:26:49 AM8/8/15
to golang-nuts
Your test is wrong.
The simplest counterexample is any executable program needs a "main" package, and it is not needed to be in the folder named "main".

Nan Xiao

unread,
Aug 8, 2015, 6:27:56 AM8/8/15
to Tamás Gulácsi, golang-nuts
Hi Tamas,

My test environment is like this:  

(1) GOPATH="/root/go";
(2) There is a folder whose name is "wire" in /root/go/src;
(3) There is a file(a.go) in wire folder:
package shark
import "fmt"

func Hello() {
        fmt.Println("Hello")
}
(4) hello.go:
package main

import (
        "shark"
)

func main() {
        shark.Hello()
}
(5) Execute "go build hello.go":  
hello.go:4:2: cannot find package "shark" in any of:
        /usr/local/go/src/shark (from $GOROOT)
        /root/go/src/shark (from $GOPATH)
(6) Rename "wire" to "shark", then compile is OK.

So except package main, other package and folder name must be same?

Thanks in advance!


Best Regards
Nan Xiao


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/oawcWAhO4Ow/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian von Kietzell

unread,
Aug 8, 2015, 6:32:21 AM8/8/15
to Nan Xiao, Tamás Gulácsi, golang-nuts

All files in a directory must be in the same package. So if a.go and hello.go are both in dir wire/ they must both be in package main. And there's no need to import shark.


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.

Axel Wagner

unread,
Aug 8, 2015, 6:38:36 AM8/8/15
to Nan Xiao, Tamás Gulácsi, golang-nuts
Hi,

Nan Xiao <xiaona...@gmail.com> writes:
> (1) GOPATH="/root/go";
> (2) There is a folder whose name is "wire" in /root/go/src;
> (3) There is a file(a.go) in wire folder:
> package shark

What you describe, is a package "shark", with the import path
"wire". So:

> (4) hello.go:

Try instead:

> package main
>
> import (
> "wire"
> )
>
> func main() {
> shark.Hello()
> }

And now you know, why there is the strong suggestion, to have the
package name and the last component of the import path the same. From
this package main, there is *no way* to deduce, that shark.Hello() is
from the package with import path "wire", except looking at the
sourcecode.

My suggestion is:
a) Name the folder /root/go/src/shark (import path "shark") and the package shark
b) Name the folder /root/go/src/wire (import path "wire") and the package wire
c) Name the folder /root/go/src/wire/shark (import path "wire/shark") and the package shark

So, you are confused by there being *three* concepts:
a) the import path
b) the location on disk
c) the package name

a and b are *strongly* related (location = $GOPATH/importpath). But c is
only related to a and b by convention.

Best,

Axel

Nan Xiao

unread,
Aug 8, 2015, 6:58:41 AM8/8/15
to Axel Wagner, Tamás Gulácsi, golang-nuts
Hi Axel,

Thanks for your comments!

So the "import" part is just "path" to find "package", it is not package name, right?

Thanks!

Best Regards
Nan Xiao

Axel Wagner

unread,
Aug 8, 2015, 9:14:40 AM8/8/15
to Nan Xiao, Tamás Gulácsi, golang-nuts
Nan Xiao <xiaona...@gmail.com> writes:
> So the "import" part is just "path" to find "package", it is not package
> name, right?

Yes :)

Nan Xiao

unread,
Aug 8, 2015, 10:32:04 PM8/8/15
to Axel Wagner, Tamás Gulácsi, golang-nuts
Hi Axel, Christian, Tamas,

Thanks very much for all your time and answer!

Best Regards
Nan Xiao
Reply all
Reply to author
Forward
0 new messages