douhui1630 2017-08-29 06:51
浏览 444

在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条回答 默认 最新

  • duankanyi6539 2017-08-29 06:59
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度