How to import packages between themselves under one repository in golang?

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

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 in sublib1.go and
  • github.com/username/repository/sublib in lib1.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.

huangapple
  • 本文由 发表于 2014年11月12日 20:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/26887117.html
匿名

发表评论

匿名网友

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

确定