This note descibes how Golang figures out where to clone a dependency from.
Go documents the functionality here: Remote import paths.
This is the short version:
When running go get git.domain.com/foo/bar
, go does the following:
- Fetch
https://git.domain.com/foo/bar?go-get=1
- Check if that page contains a meta tag like this (could also be a https url):
<meta name="go-import" content="git.domain.com git [email protected]:foo/bar.git">
- Verify that
git.domain.com
contains the same meta tag - Git clone
[email protected]:foo/bar.git
into$GOPATH/src/git.domain.com/foo
If you’re using packages hosted on an internal git instance (e.g. GitLab), you are going to be in trouble.
GitLab for example shows the https url in the meta-tag, so go get
will prompt for authentication.
The best solution would be to be able to configure GitLab to show the ssh url instead of https. Until then, here are a few approaches, choose the one that fit’s your flow.
In my opinion this is the best solution:
[url "[email protected]:"]
insteadOf = "https://git.domain.com"
Drawback: You have to configure this on every host.
You could add your own service on some domain (e.g. go.domain.com
or even
add it to your company website) which contains the correct meta tag with the ssh
url.
Drawback: You have an additional service which can break.
If you run go get git.domain.com/foo/bar.git
and import it this way, i
it will fetch the git URL without checking the <meta> tag.
Drawback: You have to remember adding .git
and it’s not really common.