douan3019 2018-12-03 06:48
浏览 217

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

Before I used go dep, but now office ensure the official tool is go mod.

When I use go dep, I can add local dependency packages to vendor and ignored in Gopkg.toml to ignore search the package from repo. I can execute go dep update normally.

Now I use go mod, I also add local dependence package to vendor and add it exclude in go.mod. But when i execute go mod tidy, it remove the package even though my project exist import xxx.

What did i do:

  1. create new project
  2. execute go mod init
  3. modify go.mod exclude privaterepo.com/bb/bb

  4. copy my local module to vendor because the local module is on a private repo which not support https.

  5. now the vendor just like:

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

  6. import "privaterepo.com/bb/bb"
  7. execute go build -mod vendor
  8. than i got error "cannot find module for path privaterepo.com/bb/bb" 9 always try with replace, but it also not work

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

  • 写回答

1条回答 默认 最新

  • doulu4316 2018-12-04 06:26
    关注

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

    Well, I think you cannot do this. This is not how it works. go mod vendor manages your vendor folder.

    Instead of excludeing you package from go.mod you should add a replace directive to instruct the go tool to look up the package not from privaterepo.com but from the local filesystem. Quoting from 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:

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

    So in your case: Do not try to manually put privaterepo.com/bb/bb in vendor, but have it somewhere outside the current project and use

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

    And let go mod copy this stuff from the filesystem to your vendor.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流