103

I started working on a Go project and it uses some private modules from Github private repos and whenever I try to run go run main.go it gives me a below 410 Gone error:

verifying github.com/repoURL/go-proto@v2.86.0+incompatible/go.mod: github.com/repoURL/go-proto@v2.86.0+incompatible/go.mod: reading https://sum.golang.org/lookup/github.com/!repoURL/go-proto@v2.86.0+incompatible: 410 Gone

I can easily clone private repo from terminal which means my ssh keys are configured correctly. I read here that I need to set GOPRIVATE environment variable but I am not sure how to do that.

Can anyone answer or point to the relevant tutorial?

Go: v1.13, OS: macOS Mojave

ifnotak
  • 4,147
  • 3
  • 22
  • 36
UsamaAmjad
  • 4,175
  • 3
  • 28
  • 35

4 Answers4

175

Short Answer:

go env -w GOPRIVATE=github.com/repoURL/private-repo

OR

If you want to allow all private repos from your organization

go env -w GOPRIVATE=github.com/<OrgNameHere>/*

Long Answer:

Check "Module configuration for non-public modules" for more information:

The GOPRIVATE environment variable controls which modules the go command considers to be private (not available publicly) and should therefore not use the proxy or checksum database. The variable is a comma-separated list of glob patterns (in the syntax of Go's path.Match) of module path prefixes. For example,

 GOPRIVATE=*.corp.example.com,rsc.io/private

causes the go command to treat as private any module with a path prefix matching either pattern, including git.corp.example.com/xyzzy, rsc.io/private, and rsc.io/private/quux.

. .

The 'go env -w' command (see 'go help env') can be used to set these variables for future go command invocations.


Note on the usage of ssh:

If you use ssh to access git repo (locally hosted), you might want to add the following to your ~/.gitconfig:

[url "ssh://git@git.local.intranet/"]
       insteadOf = https://git.local.intranet/

for the go commands to be able to access the git server.

ifnotak
  • 4,147
  • 3
  • 22
  • 36
  • 3
    Thanks! Working now so the trick was to use wildcard url with organization name. `go env -w GOPRIVATE=github.com/{OrgNameHere}/*` – UsamaAmjad Oct 09 '19 at 15:21
  • Probably easier for people who code primarily with private code `export GOPRIVATE=*` – Jay Aug 24 '20 at 03:46
  • 4
    Using `export GOPRIVATE=*` assigns all file names in the current directory to GOPRIVATE, which isn't what you want. – Dean Schulze Sep 07 '20 at 22:31
  • That is an excellent answer. Thank you for that answer. I liked that answer. – John Smith Optional Mar 14 '21 at 15:08
  • I still like that answer even now and will probably continue to like it for a long time. Maybe years. – John Smith Optional Mar 14 '21 at 15:09
  • For those of you who are looking for more details as to how `GOPRIVATE` works, etc., [Go Modules Reference](https://golang.org/ref/mod#private-modules) covers a lot more in detail. – Ryota Nov 05 '21 at 14:45
  • 5
    When executing in `zsh` the commands give `zsh: no matches found`. Escape the asterix by writing `\*` so it doesn't expand on it. – Marko Feb 02 '22 at 12:27
22

If zsh use:

go env -w GOPRIVATE='gitlab.my_firm_name.com/*'

otherwise get

zsh: no matches found: GOPRIVATE=gitlab.my_firm_name.com/*

16

Just a follow up on the usage of ssh, this is the command used to get it working:

GitHub:

git config --global url."git@github.com:".insteadOf "https://github.com/"

Bitbucket:

git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"
JFW
  • 585
  • 5
  • 7
1

If zsh use, add a / before wildcard like this: go env -w GOPRIVATE=github.com/<OrgNameHere>\/*