英文:
How to import packages between themselves under one repository in golang?
问题
我想了解如何在示例中正确导入包。
我已经阅读了这个主题(https://stackoverflow.com/questions/15049903/how-to-use-custom-packages-in-golang),但它没有解释我想要了解的内容。
例如,我想创建一个包,以便在我的程序中进一步使用,并在github.com上发布。这是我认为代码应该组织的方式:
src/
github.com/
username/
repository/
lib1.go #package repository
lib2.go #package repository
sublib/
sublib1.go #package sublib
sublib2.go #package sublib
...
myproject/
programname.go #package main
#在这里导入我的存储库没有问题:
#import "github.com/username/repository"
#或 import "github.com/username/repository/sublib"
好的,现在我想了解如何在repository/sublib
中导入repository
的代码,反之亦然。我认为这个问题更多地针对内部导入(在一个存储库下)。
第一个解决方案显然是通过完整路径导入包:
- 在
sublib1.go
中导入github.com/username/repository
,以及 - 在
lib1.go
中导入github.com/username/repository/sublib
但是,如果将来我更改路径怎么办?也许有更好的导入内部包的方法...另外,当我在sublib1.go
中导入github.com/username/repository
时,我遇到了一个问题(我收到错误消息import cycle not allowed
)。
我希望我解释得足够清楚,以便得到这个问题的答案。
英文:
I want to understand how to import packages correctly in the example beyond.
I've read this topic (https://stackoverflow.com/questions/15049903/how-to-use-custom-packages-in-golang), but it doesn't explain what I'm trying to understand.
For example, I want to create package for further using it in my program and publishing at github.com. This is how the code should be organized in my view:
src/
github.com/
username/
repository/
lib1.go #package repository
lib2.go #package repository
sublib/
sublib1.go #package sublib
sublib2.go #package sublib
...
myproject/
programname.go #package main
#there is no problem how to import my repository here:
#import "github.com/username/repository"
#or import "github.com/username/repository/sublib"
Ok, at this point I want to understand how can I import repository
's code in the repository/sublib
and vise versa. I think question is more aimed for internal importing (under one repository).
First solution is obviously - importing packages by fullpath:
github.com/username/repository
insublib1.go
andgithub.com/username/repository/sublib
inlib1.go
Hmm, but what if I change path in the future? May be there is better way for importing internal packages.. Also I'm faced with a problem when I import github.com/username/repository
in the sublib1.go
(I get the error message import cycle not allowed
).
I hope I've explained quite ok to get this question.
答案1
得分: 2
在Go语言中,不允许存在循环导入。你唯一的选择是重新考虑如何拆分你的包,以避免循环依赖。
英文:
You cannot have circular imports in Go, they are explicitly disallowed. Your only option is to rethink the way you are splitting up your packages so there are no loops.
答案2
得分: 1
import "github.com/username/repository/sublib"
是一个不错的选择:正如我在“是否可以不指定包名?”中提到的,另一种“解决方案”是使用相对路径:
import "./sublib"
这是使用相对路径的方式,但这并不被认为是一个好的实践,正如在“Go语言包结构”和“如何在Golang中导入本地包而不使用gopath?”中所解释的那样。
如果将来更改路径,无论是绝对路径还是相对路径导入的问题都会类似,但第一种(绝对路径)可以被其他人“go-get”。
英文:
import "github.com/username/repository/sublib"
is the way to go: as I mentioned in "Is it possible don't specify package name?", the other "solution" would be
import "./sublib"
That is using a relative path, which is not considered a good practice, as explained in "Go language package structure" and "Golang how to import local packages without gopath?".
If you change path in the future, the issue will be similar with absolute or relative import paths, but the first (absolute) paths remains "go-gettable" by other people.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论