英文:
Go (golang) Package consisting of several folders
问题
在Go(golang)中,是否可以将一个包组成多个来自不同文件夹的.go文件?
我试图在主文件夹内创建一个子文件夹,但编译器显示找不到定义("undefined"错误)。当所有文件都在同一个文件夹中时,编译不会出错。
谢谢。
英文:
Is it possible in Go (golang) to have a package consisting of several .go files from different folders?
I am trying to make a subfolder inside of the main folder, and compiler says that it cannot find definitions ("undefined" error). When all the files are in the same folder, compilation does not give errors.
Thanks.
答案1
得分: 38
不,这是不可能的。
如果你想在你的Go项目中使用文件夹,你必须使用多个包。
无效的结构
myproject
| -- main.go (包名:main)
| -- routes.go (包名:main)
+ -- models
| -- db.go (包名:main)
| -- mymodel.go (包名:main)
有效的结构
myproject
| -- main.go (包名:main)
| -- routes.go (包名:main)
+ -- models
| -- db.go (包名:models)
| -- mymodel.go (包名:models)
英文:
No, this is not possible.
If you want to use folders inside your go project you have to use several packages.
NOT VALID
myproject
| -- main.go (package main)
| -- routes.go (package main)
+ -- models
| -- db.go (package main)
| -- mymodel.go (package main)
VALID
myproject
| -- main.go (package main)
| -- routes.go (package main)
+ -- models
| -- db.go (package models)
| -- mymodel.go (package models)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论