去不工作/去另一个地方去

I'm trying to go get a project that was initially hosted on a defunct/unavailiable A.com server. The same project is now served under B.com but all imports inside the code are pointing to A.com (eg. import A.com/user/projet/lib).

When I try to go get B.com/user/projet it's cloning the git repo from B.com but then tries to download resources from A.com.

How can tell go get that the package A.com/user/projet is now served at B.com/user/projet (without rewriting imports inside the code) ?

There is another dirty trick no one mentions - manually git clone, git pull package in $GOPATH/src/A.com/package, before maintainer will rewrite imports himself. go build, go install work as expected. I do this time to time as quick hack.

This very hard to do.

As suggested before by myself and @jimb it would be easier to re-write the paths. However there is something you can do.

When the import path is not a know code hosting site

The text is the out put of the the command go help importpath

If the import path is not a known code hosting site and also lacks a version control qualifier, the go tool attempts to fetch the import over https/http and looks for a <meta> tag in the document's HTML <head>.

The meta tag has the form:

<meta name="go-import" content="import-prefix vcs repo-root">

run go help importpath for the full information.


What all of that means

Basically that means you can import your code base using the old import address but it will clone from the repository provided by the meta tag

However you would need the original place to host a page with that meta tag.

Not ideal

But the Golang team has been know to use that see the below example.

$ curl golang.org/x/tools/cmd/rename
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="golang.org/x/tools git https://go.googlesource.com/tools">
<meta name="go-source" content="golang.org/x/tools https://github.com/golang/tools/ https://github.com/golang/tools/tree/master{/dir} https://github.com/golang/tools/blob/master{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/golang.org/x/tools/cmd/rename">
</head>
<body>
Nothing to see here; <a href="https://godoc.org/golang.org/x/tools/cmd/rename">move along</a>.
</body>
</html>

The go-import meta tag tells the go get where to fetch it from and go-source where the source code is to browsing.

Please see below a useful article about this very feature.

Golang Canonical Import Paths

Other more questionable tricks

Proxying and/or host file entries

Other very dirty tricks would be to proxy the server in question (using something like nginx and host entries) or just use the host file to point the url to the new location but every time you clone you will need to have that host entry.

Not recommended

Vendor the package and trick go get with git submodules

Another option is to vendor the package in your source repository so go get will never attempt to import the package again and it will ignore what ever you have on your gopath.

If you add the package to your repository as a submodule to the new location you can still have both code bases separate but because the imported package will be in your vendor folder go get will never attempt to import anything.

In my opinion the lesser then two evils if you can not do the go get meta tags

I would suggest that you have a look at git sub-tree as a alternative to git sub-modules. They are pretty cool and easier to deal with.


I hope this helps.