旧行李 2013-07-09 03:28 采纳率: 25%
浏览 340
已采纳

戈朗如何进口本地包没有 gopath?

I've used GOPATH but for this current issue I'm facing it does not help. I want to be able to create packages that are specific to a project:

myproject/
├── binary1.go
├── binary2.go
├── package1.go
└── package2.go

I tried multiple ways but how do I get package1.go to work in the binary1.go or the binary2.go and so on?

For example; I want to be able to import "package1" and then be able to run go build binary1.go and everything works fine without the error being thrown that the package cannot be found on GOROOT or GOPATH. The reason why I need this kind of functionality is for large scale projects; I do not want to have to reference multiple other packages or keep them in one large file.

转载于:https://stackoverflow.com/questions/17539407/golang-how-to-import-local-packages-without-gopath

  • 写回答

4条回答 默认 最新

  • Lotus@ 2013-07-09 03:43
    关注

    Go dependency management summary:

    • vgo if your go version is: x >= go 1.11
    • dep or vendor if your go version is: go 1.6 >= x < go 1.11
    • Manually if your go version is: x < go 1.6

    Edit 3: Go 1.11 has a feature vgo which will replace dep.

    To use vgo, see Modules documentation. TLDR below:

    export GO111MODULE=on
    go mod init
    go mod vendor # if you have vendor/ folder, will automatically integrate
    go build
    

    This method creates a file called go.mod in your projects directory. You can then build your project with go build. If GO111MODULE=auto is set, then your project cannot be in $GOPATH.


    Edit 2: The vendoring method is still valid and works without issue. vendor is largely a manual process, because of this dep and vgo were created.


    Edit 1: While my old way works it's not longer the "correct" way to do it. You should be using vendor capabilities, vgo, or dep (for now) that are enabled by default in Go 1.6; see. You basically add your "external" or "dependent" packages within a vendor directory; upon compilation the compiler will use these packages first.


    Found. I was able import local package with GOPATH by creating a subfolder of package1 and then importing with import "./package1" in binary1.go and binary2.go scripts like this :

    binary1.go

    ...
    import (
            "./package1"
          )
    ...
    

    So my current directory structure looks like this:

    myproject/
    ├── binary1.go
    ├── binary2.go
    ├── package1/
    │   └── package1.go
    └── package2.go
    

    I should also note that relative paths (at least in go 1.5) also work; for example:

    import "../packageX"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c