英文:
How to make a short/relative import
问题
在Go代码中,可以使用相对导入吗?我看了很多例子,但我真的不明白如何做到。例如,我有一个名为app
的项目和一个子包utils
。
app
main.go
utils
utils.go
utils.go:
package utils
import "fmt"
func TestFunc() {
fmt.Print("I'm a TestFunc")
}
有没有办法只使用import "./utils"
或import "app/utils"
来导入这个包,而不是像import "github.com/hithubuser/app/utils"
那样写全路径?
如果没有办法,你是如何处理嵌套包的?你是否为所有导入都写全路径,还是避免这样做?
英文:
Is it possible to make a relative import in Go code? I read a lot of examples but I really can't understand how to make it. For example, I have a project app
and a sub package utils
in it.
app
main.go
utils
utils.go
utils.go:
package utils
import "fmt"
func TestFunc() {
fmt.Print("I'm a TestFunc")
}
Is there a way to import this package using only import "./utils"
or import "app/utils
but not take all the path like import "github.com/hithubuser/app/utils"
?
If there is no way to do it, how do you work with the nested packages? Do you write a full the path for all the imports or you avoid to make it?
答案1
得分: 4
通常,导入路径从$GOPATH/src
之后开始,可以参考文档。
假设你有以下目录结构:
$GOPATH/src/github.com/hithubuser/app/utils
# 你的导入路径是 'github.com/hithubuser/app/utils'
你是否为所有导入写全路径,还是避免写全路径?
是的,通常使用完整的导入路径,因为这样可以将代码作为Go库进行分发。这样你就可以开发可重用的代码,并根据需要在任何项目中导入它,或者将其发布给Go社区。
假设你有一个独立的项目,不依赖于任何其他Go项目,并且你将其作为二进制文件而不是Go库进行分发。那么你可以像这样做(尽管这不常用/不推荐)。
只需将你的仓库克隆到$GOPATH/src
,例如 git clone github.com/hithubuser/app
$GOPATH/src/app/utils
# 然后导入路径是 app/utils
英文:
Typically import path starts after $GOPATH/src
, refer to doc.
Let's say you have directory structure:
$GOPATH/src/github.com/hithubuser/app/utils
# your import path is 'github.com/hithubuser/app/utils'
> Do you write a full the path for all the imports or you avoid to make it?
Yes, generally full import is used, since it enables you to distribute your code as go library. So that you can develop reusable code and import it in any project as needed or you can publish it to Go community.
Let's say you have standalone project, not referred to any other Go project and you're distributing as binary instead of go library. Then you can to something like this (it is not commonly used/recommended though).
Just git clone your repo at $GOPATH/src
like git clone github.com/hithubuser/app
$GOPATH/src/app/utils
# then import path is app/utils
答案2
得分: 1
另一种可能性是使用Go 1.5引入的供应商功能。
-
创建一个
vendor
目录。 -
将您的
utils
目录复制(或git clone
或其他方式)作为vendor
的子目录,这样您的层次结构现在看起来像这样:<!-- language: none -->
app |-- main.go `-- vendor/ `-- utils/ `-- utils.go
-
如果您使用的是Go 1.5,请将
GO15VENDOREXPERIMENT
环境变量设置为1以启用供应商支持。更新版本默认启用该功能。
英文:
Another possibility is to use the vendor facilities introduced in Go 1.5.
-
Create a
vendor
directory. -
Copy (or
git clone
or whatever) yourutils
directory as a subdirectory ofvendor
, so your hierarchy now looks like this:<!-- language: none -->
app |-- main.go `-- vendor/ `-- utils/ `-- utils.go
-
Set the
GO15VENDOREXPERIMENT
environment variable to 1 to enable vendoring support if you're using Go 1.5. Newer versions enable it by default.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论