Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active January 2, 2021 23:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/519c18f994000dcb60aa1489c44b6913 to your computer and use it in GitHub Desktop.
Save xeoncross/519c18f994000dcb60aa1489c44b6913 to your computer and use it in GitHub Desktop.
Access private repositiories in Go projects hosted on a remote server (using SSH)

Setup private Go modules

You have a git repositiory on a remote server. This is probably a VPS you have online at Digital Ocean, AWS, or some other hosting company. Could also be a docker container or raspberrypi under your desk.

Regardless, you can include the repositiory in your go projects by helping git (which Go uses) to know how to access the code over SSH.

Edit your ~/.gitconfig and make sure your host is listed. Below are examples for github, bitbucket, and my own custom VPS.

[url "git@github.com:"]
        insteadOf = https://github.com/
[url "git@bitbucket.org:"]
        insteadOf = https://bitbucket.org/
[url "johnsmith@xeoncross.com:"]
        insteadOf = https://xeoncross.com/

Then you can checkout the repo as a go module. Note, you have to use the .git extension.

go get -v -x xeoncross.com/repos/lib

Next you need to setup Module configuration for non-public modules (disable proxy sum checking)

go env -w GOPRIVATE=xeoncross.com,github.com/privateorg

or whole paths on github

go env -w GOPRIVATE=github.com/usernamespace

Now you can use the module in your import paths:

import "xeoncross.com/repos/golib.git/a"

Related issues and discussions:

Relative Paths

If you are writing multiple sibling modules at the same time it could be beneficial to use relative paths in your modules so you don't have to commit changes and update the dependent module for each change.

In this case, you can use the replace directive.

replace also can be used to inform the go tooling of the relative or absolute on-disk location of modules in a multi-module project, such as:

replace example.com/project/foo => ../foo

Instead of editing your go.mod file directly use the go mod command:

 go mod edit -replace xeoncross.com/repos/golib.git=/Users/owner/path/here

You can read more here: https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment