How to set GOPRIVATE environment variable
Asked Answered
S

4

110

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/[email protected]+incompatible/go.mod: github.com/repoURL/[email protected]+incompatible/go.mod: reading https://sum.golang.org/lookup/github.com/!repoURL/[email protected]+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

Shawannashawl answered 9/10, 2019 at 13:42 Comment(8)
man $(basename $SHELL)Alderney
There's a go help for that: go help module-privateFabrienne
Or just to make live easier, export GOPRIVATE=*Hewett
That's an excellent question.Valdemar
I was gonna ask how to add multiple path when exporting GOPRIVATE. I've tried export GOPRIVATE="github.com/repo01;gitlab.com/repo02" but didn't work. But, after looking @Hewett 's comment, I tried export GOPRIVATE=* and it just works 😂 Btw, I think the go help for that right now is moving to go help privatePrivative
@PikoMonde Yep, that works, or use a comma as the separator.Hewett
@PikoMonde as per Jay's comment. The key bit from the doc: "Comma-separated list of glob patterns"Microampere
The help topic for this is 'go help private' contrary to a comment above.Skylark
C
185

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://[email protected]/"]
       insteadOf = https://git.local.intranet/

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

Countrywide answered 9/10, 2019 at 14:5 Comment(7)
Thanks! Working now so the trick was to use wildcard url with organization name. go env -w GOPRIVATE=github.com/{OrgNameHere}/*Shawannashawl
Probably easier for people who code primarily with private code export GOPRIVATE=*Hewett
Using export GOPRIVATE=* assigns all file names in the current directory to GOPRIVATE, which isn't what you want.Java
That is an excellent answer. Thank you for that answer. I liked that answer.Valdemar
I still like that answer even now and will probably continue to like it for a long time. Maybe years.Valdemar
For those of you who are looking for more details as to how GOPRIVATE works, etc., Go Modules Reference covers a lot more in detail.Subedit
When executing in zsh the commands give zsh: no matches found. Escape the asterix by writing \* so it doesn't expand on it.Cannon
J
29

If zsh use:

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

otherwise get

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

Jazmin answered 13/5, 2022 at 18:21 Comment(2)
this resolved the error for zsh thanksHileman
Works for fish shell as wellGodbeare
T
16

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

GitHub:

git config --global url."[email protected]:".insteadOf "https://github.com/"

Bitbucket:

git config --global url."[email protected]:".insteadOf "https://bitbucket.org/"
Transfiguration answered 10/7, 2020 at 11:36 Comment(0)
C
2

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

Cysto answered 11/4, 2023 at 23:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.