使用Go Mod时如何向供应商添加本地依赖项

How to add local dependence to vendor when using go mod

在使用go dep之前,但现在Office确保官方工具为go mod

使用go dep时,可以将本地依赖软件包添加到供应商,而在Gopkg.toml中的ignored中可以忽略从回购中搜索软件包。我可以正常执行go dep update

现在,我使用go mod,还将本地依赖程序包添加到供应商,并将其添加到go.mod中。但是,当我执行go mod tidy时,即使我的项目存在import xxx,它也会删除程序包。

我做了什么:

  • 创建新项目
  • 执行go mod init
  • 修改go.mod exclude privaterepo.com/bb/bb

  • 将我的本地模块复制到供应商,因为本地模块在不支持https的私有存储库中。

  • 现在供应商就像:

    vendor
    |-github.com/aa/aa
    |-privaterepo.com/bb/bb

  • 导入" privaterepo.com/bb/bb"
  • 执行go build -mod供应商
  • 比我得到的错误"找不到路径privaterepo.com/bb/bb的模块"
    9总是尝试用replace,但是也不行
  • 因此,我该怎么做才能将本地软件包添加到供应商,并避免go mod删除它?


    So, what should I do to add local package to vendor and avoid go mod remove it?

    好吧,我认为您不能这样做。 这不是它的工作方式。 go mod vendor管理您的供应商文件夹。

    与其从go.mod中exclude打包,不如添加一个replace指令来指示go工具不是从privaterepo.com而是从本地文件系统中查找软件包。 从https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive引用

    replace also can be used to inform the go tooling of the relative or absolute on-disk location of modules in a multi-module project, such as:

    1
       replace example.com/project/foo => ../foo

    因此,在您的情况下:请勿尝试将privaterepo.com/bb/bb手动放置在供应商中,而应将其放置在当前项目之外并使用

    1
    replace privaterepo.com/bb/bb => ../bb

    然后让go mod将这些内容从文件系统复制到您的供应商。