How to use 'go get' or 'go mod vendor' for a specific module, without trying to update other modules?

ozma picture ozma · Jul 23, 2020 · Viewed 22.1k times · Source

I'm trying to get a specific package from github for a project.

However, when I use go get [url] or go mod vendor, I get a git fetch error for lack of permissions to one of my company's repos. This repo is vendored, which is how we get around it for go test, go build etc.

This is the error message:

go: private.work.repo.com/project/[email protected]: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in C:\Users\NICHOLAS.TAN\go\pkg\mod\cache\vcs\37594aeb10b98234e04b4780cf59f32c4ad7bb9da460f552103ae748cea73aa1: exit status 128:
        fatal: remote error: Repository not found
        The requested repository does not exist, or you do not have permission to
        access it.

Is there a way for me to use go get and/or go mod vendor without those commands trying to look at the other module dependencies?

Answer

kozmo picture kozmo · Jul 23, 2020

You can get specific version of package use go get <package>@<version> in your project directory, for example:

% go get github.com/golang/[email protected] 

go: downloading github.com/golang/protobuf v1.4.0

Go download only get github.com/golang/protobuf package required version to local cache ($GOPATH/pkg/mod) and set version to go.mod file.

After all, if you have dependencies from your company's repos in local cache ($GOPATH/pkg/mod), use go mod vendor to create vendor (Go get ones from cache).