在Go语言中导入包路径问题

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

Import package Path issues in Go

问题

我正在尝试在Go语言中构建一个集合包,并且在导入路径方面遇到了问题。

我正在建模Java的Collections接口。这是我的文件结构:

+/$GOPATH
    /bin  
    /pkg  
    /src  
        /github.com
            /user
               /collections
                   collections.go
                   main.go
                  /collections
                      /bstAvl
                         bstAvl.go

我的collections.go文件如下所示:

package collections

type Collection interface {
    Add(interface{}) (bool, error)
    AddAll(Collection) (bool, error)
    Clear()
    Contains(interface{}) (bool, error)
    Remove(interface{}) (bool, error)
    Size() uint
}

bstAvl.go中,我想要使用Collections.go中的Collection接口。

我应该如何导入所需的包以访问Collection接口?

我觉得我在路径方面可能有些过度,可能使它比应该的更复杂了。你推荐的简单结构是什么?

理想情况下,我希望所有的集合都在collections包下,这样它可以作为一个库导出,并在其他应用程序中使用(如果需要的话)。

附注:我已经阅读了Ben Johnson的《在Go中构建应用程序的结构》。但我仍然感到困惑。非常感谢任何帮助。

编辑:
我想我在包结构上做得太多了。我决定坚持这个结构:

+/$GOPATH
    /bin  
    /pkg  
    /src  
        /github.com
            /user
               /collections
                   collections.go
                   main.go
                   bstAvl.go
英文:

I am trying to build a collections package in Go, and I am having trouble with import paths.

I am modeling the Java Collections interface. Here is my file structure

+/$GOPATH
    /bin  
    /pkg  
    /src  
        /github.com
            /user
               /collections
                   collections.go
                   main.go
                  /collections
                      /bstAvl
                         bstAvl.go

My collections.go file looks like this:

package collections

type Collection interface {
    Add(interface{}) (bool, error)
    AddAll(Collection) (bool, error)
    Clear()
    Contains(interface{}) (bool, error)
    Remove(interface{}) (bool, error)
    Size() uint
}

In bstAvl.go i am trying to use the Collection interface in Collections.go

How do I import the required package to access the Collection interface?

I think I have gone overboard with the paths, probably made it more complicated than it should be. Is the simpler structure you recommend?

Ideally, I would want all my collections to be under the collections package so that it could be exported as a library and used in other applications if need be.

P.S I have read Structuring applications in Go by Ben Johnson. But I am still confused. Any help is greatly appreciated.

EDIT:
I guess i over worked the package structure. I have decided to stick with this:

+/$GOPATH
    /bin  
    /pkg  
    /src  
        /github.com
            /user
               /collections
                   collections.go
                   main.go
                   bstAvl.go

答案1

得分: 2

要在bstAvl.go中导入collections,请使用完整路径:

import "github.com/user/collections"

至于结构方面,这完全取决于包的类型,我对Java的集合不够了解,无法在这个特定情况下给出建议。

但是,为了避免出现重复的collections/collections,我建议将bstAvl文件夹直接放在初始的collections文件夹下:

+/$GOPATH
    /bin  
    /pkg  
    /src  
        /github.com
            /user
               /collections
                   collections.go
                   main.go
                   /bstAvl
                      bstAvl.go
英文:

To import collections inside bstAvl.go, use the full path:

import "github.com/user/collections"

When it comes to structure, it all depends on types of package, and I do not know Java collections enough to give advice in this specific case.

However, to avoid getting a repeating collections/collections, I would suggest placing the bstAvl folder directly under the initial collections-folder:

+/$GOPATH
    /bin  
    /pkg  
    /src  
        /github.com
            /user
               /collections
                   collections.go
                   main.go
                   /bstAvl
                      bstAvl.go

huangapple
  • 本文由 发表于 2017年8月29日 14:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/45932523.html
匿名

发表评论

匿名网友

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

确定