在重命名后使用相对包导入的 Golang 代码。

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

Golang relative package import after renaming

问题

我的$GOPATH是"/Users/peter/goworkspace"。

我的当前golang版本是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(
"./package1"
"./package2"
"./package3"
)

然而,当我运行"go build"时,我一直得到错误提示:

"can't load package: local import "../package" in non-local package"

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

import(
"project1/package1"
"project1/package2"
"project1/package3"
)
那么一切都能正常工作。

如果我使用相对包路径,我的代码有什么问题?
如果将来将项目1的名称更改为projecet1v2,包的导入最佳实践是什么?
我需要手动更新导入的包的名称吗?

英文:

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?

答案1

得分: 5

规则#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)
英文:

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(
    &quot;project1/package1&quot;  
    &quot;project1/package2&quot;   
    &quot;project1/package3&quot;
    // Or ideally, so others can access it in the future:
    &quot;github.com/&lt;yourusername&gt;/project1/package4&quot;
)  

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)

huangapple
  • 本文由 发表于 2016年2月29日 04:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/35688100.html
匿名

发表评论

匿名网友

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

确定