重命名后Golang相对包的导入 [英] Golang relative package import after renaming

查看:1022
本文介绍了重命名后Golang相对包的导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的$ GOPATH是
$ b $ pre $ / Users / peter / goworkspace



我目前的golang版本:

  go version go1 .6 darwin / amd64 

我在这个工作区下有多个golang项目,所以这里是目录

  + / goworkspace 
+ --- / bin
+ --- / pkg
+ --- / src
+ --- / project1
+ --- package1
+ --- file1.go
+ --- file2.go
+ --- file3.go
+ --- package2
+ --- package3
+ --- main.go
+ --- / project2
+ --- / project3

在我的proj1的main.go中,我将使用import从该项目下的其他软件包中,它将如下所示:

  import(
./package1
./package2
./package3

然而, WH我运行去建立,我不断收到错误说:


无法加载包:local import../packagein non-local package



如果我不想使用相对包路径,例如使用它:

<$ p
$ project $ b $ project $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ c $ c>

那么一切都会奏效。



如果使用相对包路径,我的代码有什么问题?

如果project1的名称会在将来改变,例如改为projecet1v2?

我需要手动更新导入的软件包名称吗?

解决方案规则#1:不要使用相对导入。这是(部分)为什么你遇到问题。仔细阅读: https://golang.org/doc/code.html#Library



使用完全限定的导入路径(如您所示):

  import(
project1 / package1
project1 / package2
project1 / package3
//理想情况下,其他人可以在将来访问它:
github.com/<yourusername>/project1/package4

如果由于某种原因,您想要对您的软件包进行版本化,您可以:


  • 提供新的存储库(导入URL)

  • 使用像gopkg.in这样的服务( http://labix.org/gopkg.in)提供版本化的导入URL(例如gopkg.in/you/pkgname.v2)

    My $GOPATH is

    "/Users/peter/goworkspace"
    

    My current golang version:

    go version go1.6 darwin/amd64
    

    I have multiple golang projects under this workspace, so here is the the structure of directories

    +/goworkspace  
      +---/bin  
      +---/pkg  
      +---/src  
        +---/project1
            +---package1 
               +---file1.go 
               +---file2.go 
               +---file3.go 
            +---package2 
            +---package3 
            +---main.go 
        +---/project2
        +---/project3
    

    In my proj1's main.go, I will use the imports from other packages under this project, it will look like:

    import(  
         "./package1"      
         "./package2"      
         "./package3"
     )  
    

    however when I run "go build",I'm keeping getting error saying:
    " can't load package: local import "../package" in non-local package

    If I prefer not to use relative package path, for example use it like:

    import(
        "project1/package1"  
        "project1/package2"   
        "project1/package3"   
    )  
    

    then everything will work.

    What is wrong with my code if I use the relative package path?
    And what is the best practice for package import if the project1's name will change in the future, for example it is changed to projecet1v2?
    Do I need to manually update the imported package's name then?

    解决方案

    Rule #1: Don't use relative imports. This is (partly) why you're running into issues. Read through this: https://golang.org/doc/code.html#Library

    Use the fully qualified import paths (as you showed):

    import(
        "project1/package1"  
        "project1/package2"   
        "project1/package3"
        // Or ideally, so others can access it in the future:
        "github.com/<yourusername>/project1/package4"
    )  
    

    If for some reason you want to version your package, you can either:

    • Provide a new repository (import URL)
    • Use a service like gopkg.in (http://labix.org/gopkg.in) to provide versioned import URLs (e.g. gopkg.in/you/pkgname.v2)

    这篇关于重命名后Golang相对包的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆