首页 > 解决方案 > How to update imports when they change location

问题描述

I see that in Go you can import packages directly from Github like:

import "github.com/MakeNowJust/heredoc"

I understand that the path I am seeing in import line is not an URL, but only the path the package is located in (normally relative to $GOROOT/src/pkg or $GOPATH/src). So the package heredoc is most probably located in the directory $GOPATH/src/github.com/MakeNowJust/heredoc.

Now let's say that the package developer decided to migrate the code repo to Bitbucket. So now the library URL is bitbucket.com/muchMoreCoolerName/heredoc. He also added some new features to the code repo.

My question is how will you get the updated code?

The only solution I can think of is changing all the imports to new URL and doing go get again. But changing the code for library update seems a little inconvenient.

标签: gopackage

解决方案


如果您只是使用 go get 然后导入,则无法绕过它,您将不得不更新导入路径以获取新代码。但是,如果您使用供应商(一种使您的代码保持依赖关系并与它们一起分发的技术),那么至少在您更新之前,您将与该移动隔离。当您想要更新时,您可以使用供应商功能来保留旧的导入路径,但要与其他 repo 同步。

坦率地说,无论如何我仍然会使用供应商,当我决定更新时,只需搜索并替换旧的导入路径,这并不难。

编辑如果您还没有转换到模块,您还可以使用dep来管理依赖项。


推荐阅读