如何进行短路径/相对路径导入。

huangapple go评论74阅读模式
英文:

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引入的供应商功能。

  1. 创建一个vendor目录。

  2. 将您的utils目录复制(或git clone或其他方式)作为vendor的子目录,这样您的层次结构现在看起来像这样:

    <!-- language: none -->

     app
     |-- main.go
     `-- vendor/
         `-- utils/
             `-- utils.go
    
  3. 如果您使用的是Go 1.5,请将GO15VENDOREXPERIMENT环境变量设置为1以启用供应商支持。更新版本默认启用该功能。

英文:

Another possibility is to use the vendor facilities introduced in Go 1.5.

  1. Create a vendor directory.

  2. Copy (or git clone or whatever) your utils directory as a subdirectory of vendor, so your hierarchy now looks like this:

    <!-- language: none -->

     app
     |-- main.go
     `-- vendor/
         `-- utils/
             `-- utils.go
    
  3. Set the GO15VENDOREXPERIMENT environment variable to 1 to enable vendoring support if you're using Go 1.5. Newer versions enable it by default.

huangapple
  • 本文由 发表于 2017年7月17日 08:01:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/45134378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定