dongshike7171 2018-11-18 21:50
浏览 253
已采纳

在本地将子包与go mod一起使用

I have a go package located on my filesystem (not in the $GOPATH), called bitbucket.org/me/awesome.

~/awesome> tree
.
├── main.go
├── go.mod
├── go.sum
├── subpackageA
│   └── main.go

My go.mod looks like:

module bitbucket.org/me/awesome

require (
       ... # lots of external dependencies
)

replace bitbucket.org/me/awesome => ./

In main.go in my top-level directory, I call a subpackage like follows:

import "bitbucket.org/me/awesome/subpackageA"

which all seems pretty normal. go get works. However, when I clone this entire repository somewhere else (say in a Docker image) and run go get for the first time, I get errors like:

package bitbucket.org/me/awesome/subpackageA: https://api.bitbucket.org/2.0/repositories/me/awesome?fields=scm: 403 Forbidden,

which means it's not using the local filesystem version of the packages, even though I told it to with the replace directive in the go.mod file.

What am I doing wrong? How do I ensure that subpackages are used from the filesystem instead of attempting to be fetched from the internet?

  • 写回答

1条回答 默认 最新

  • duang5049 2018-11-19 09:28
    关注

    Go has no (real) notion of "subpackage". All packages are basically treated equal. This means that a replace bitbucket.org/me/awesome does not influence package bitbucket.org/me/awesome/subpackageA as these are two individual, unrelated packages. The folder layout does not introduce a relation of subpackageA to awsome, or the other way around *).

    So you need to add an individual replace directive for subpackageA

    replace bitbucket.org/me/awesome/subpackageA => ./subpackageA
    

    *) Nitpicking for absolute correctness: Folder layout does have influence for folders named internal (cannot be imported from other projects), for folders named vendor (which may contain vendored packages) and searching for a go.mod file stops at the repo root.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错