假设我有一个如下的包文件夹:
hello
|---main.go
|---utilities.go
在main.go
中,我有:
package main
在utilities.go
中,我有:
package hello
我这样做是因为:
- 我不想将所有实用程序放入单个
main.go
文件中。 - 这些实用程序仅由该包使用,因此我不想将其放在
hello
文件夹之外。
但是当我运行 go list hello
时,它给了我这个:
can't load package: package hello: found packages main (main.go) and hello (utilities.go) in E:\Workbench\Go\src\hello
然后我删除了 utilities.go
并仅使用 main.go
再次尝试了 go list hello
。它给了我这个:
hello
那么我是否必须将所有实用程序移动到另一个包文件夹中?
我是否必须在 hello 包文件夹中只保留单个 main.go
因为它似乎与其他包名称不能很好地相处?
添加1
有用的引用:http://thenewstack.io/understanding-golang-packages/
添加2
经过一些简短的实验,我猜 import yyy
决定了链接搜索路径,而 package xxx
声明决定了编译后的 中导出的符号名称。一个
文件。
因为我注意到这些事实:
package xxx
声明不必与包含的包文件夹名称相同,例如yyy
。- 但是
import yyy
声明必须使用包文件夹路径yyy
。 - 使用包中的符号时,我们仍然必须使用
xxx.Symbol
而不是yyy.Symbol
。
添加3
为了证实我对 ADD 2 的猜测,我这样做了:
(pkg1\file1.go)
package pkg2 // I deliberately use a different pkg name "pkg2" rather than the folder name "pkg1"
import "fmt"
func FA() {
fmt.Println("in the pkg2.FA")
}
(cmd1\main.go)
package main
import "pkg1"
func main() {
pkg2.FA() //here, the FA is referenced via "pkg2" rather than the "pkg1".
}
以上2个文件可以编译运行。
但是如果我改成这样:
(cmd1\main.go)
package main
import "pkg1"
func main() {
pkg1.FA() //here, the FA is referenced via "pkg1" as in the import statement. Should work, isn't it?
}
这将给出:
....\src\cmd1\main.go:3: imported and not used: "pkg1" as pkg2
....\src\cmd1\main.go:6: undefined: pkg1 in pkg1.FA
为了再次确认,我使用 go tool nm pkg1.a
检查了编译后的 pkg1.a
文件,我没有看到包名称影响导出的符号。 (顺便说一句,GNU nm 工具似乎无法识别 golang .a
文件格式。)
U
515 T %22%22.FA <========== this is my fuction FA
67b R %22%22.FA·f <========== this is my fuction FA
5eb T %22%22.init
67b B %22%22.initdone·
683 R %22%22.init·f
U fmt.Println
U fmt.init
673 R gclocals·33cdeccccebe80329f1fdbee7f5874cb
66b R gclocals·69c1753bd5f81501d95132d08af04464
65b R gclocals·e29b39dba2f7b47ee8f21f123fdd2633
64d R go.string."in the pkg2.FA"
63d R go.string.hdr."in the pkg2.FA"
7d2 R go.typelink.*[1]interface {}
796 R go.typelink.[1]interface {}
737 R go.typelink.[]interface {}
U runtime.algarray
U runtime.convT2E
6ec R runtime.gcbits.01
68b R runtime.gcbits.03
U runtime.morestack_noctxt
U runtime.throwinit
79a R type.*[1]interface {}
7d6 R type..importpath.fmt.
73b R type..namedata.*[1]interface {}.
6ed R type..namedata.*[]interface {}.
68c R type..namedata.*interface {}.
74e R type.[1]interface {}
6ff R type.[]interface {}
69c R type.interface {}
U type.string
所以到目前为止的结论是:
当声明的包名称与包文件夹名称不同时,包声明将用作包的备用名称。
与此等效,但隐含:
import pkg2 "pkg1"
顺便说一句,由于包声明没有反射(reflect)在编译的 .a
文件中,我猜 golang 编译需要 .go
文件的存在。
最佳答案
同一包AKA文件夹中的go文件必须具有相同的包名称。唯一的异常(exception)是可以具有 _test 扩展名的测试。规则是 1 个文件夹,1 个包,因此同一文件夹中的所有 go 文件具有相同的包名称。
答案是肯定的,将“hello”包文件移动到自己的文件夹中,或者,对同一目录中的所有文件使用相同的包名称。
关于go - 如果包文件夹中存在多个文件且其中一个文件包含 "package main",如何指定包名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40868340/