Using go get to retrieve modules / packages from a private server

Go recognizes some public servers like github.com and bitbucket.org. I’m trying to use go get with a private server. I can clone my repo with git:

git clone user@10.xxx.yyy.zzz:/srv/git/mymodule.git

I’ve gotten “go get” to work with this command:

go get 10.204.101.186/srv/git/mymodule.git/controller.service

but the command then fails with

package 10.204.101.186/srv/git/mymodule.git/controller.service: no Go files in /home/dean/src/golang/go3p/src/10.204.101.186/srv/git/mymodule.git/controller.service

What “go get” is doing is downloading the whole path from the server, including the .git/ directory.
This also kills the possibility of keeping a git module on a private server.

I haven’t found any documentation on using a private server. The “go get” command seems to only recognize certain public servers.

Does anyone know how to use the go tool chain with packages or modules stored on a private server?

At my working place we are successfully using a self hosted gitlab instance, with public and private repositories.

Though go get will use HTTPS transfer only to clone. you need to add “rewrites” to git to enable SSH transfer.

THe following snippet will rewrite HTTPS access to example.com with SSH access:

[url "git@example.com:"]
        insteadOf = http://example.com/

This of course assumes, that you have set up your keys correctly in SSH :smiley:

I hope this answeres your question, as I dont know what problem you are describing otherwise…

PS: Do you actually have .go files in the 10.204.101.186/srv/git/mymodule.git/controller.service package?

PPS: I probably wouldn’t expose the full path to the repository in the package name, but expose it with a shorter path.

Can you give an example of an import and a go get statement that work with your private repo?

I have .go files under 10.204.101.186/srv/git/mymodule.git/controller.service in the src/ directory like normal. The go get command pulls down the whole directory structure under the first entry in my GOPATH (including srv/git/mymodule.git instead of just mymodule/).

I have rsa key access to the server.

10.204.101.186/srv/git/mymodule.git/controller.service is your package name. You have to import it by that name, eg:

import "10.204.101.186/srv/git/mymodule.git/controller.service"

In a GOPATH legacy style environment, this will of course create the folder $GOPATH/src/10.204.101.186/srv/git/mymodule.git/controller.service on your system.

This is how gos legacy package system works.

If you want to have it differently, you need to reconfigure your git server to serve from a different root.

You probably want to configure it, such that 10.204.101.186/mymodule.git/controller.service points to your service.

Also, I do not quite understand what you mean by “I have .go files under 10.204.101.186/srv/git/mymodule.git/controller.service in the src/ directory like normal”. Do you want to say that the path points to a GOPATH like directory structure? That is not how go packages work. Take a look at a random go project at github, bitbucket, gitlab, etc, none of them has a src folder.

1 Like

10.204.101.186 is the server IP address. I have to use the server IP address because if I use the server name (in /etc/hosts) go get thinks the server name is part of a package name or path.

/srv/git/ is the directory where my git repo is (mymodule.git). controller.service/ is a directory under which I have the src/ directory and under src/ I have cmd/ and internal/ directories where the .go files are. The package name under the internal/controller-service/ directory is “controller”.

Just about every git project I’ve written has a src/ directory under which are other directories containing .go files. All the .go files in one of those subdirectories belong to the same package. That’s how go packages have always worked.

The server name is part of the package name! This is how packages in go work.

Yes, thats true and I won’t object.

So it does not contain a GOPATH like structure, thats fine.

Though then the package to go get is the one that contains your go files. You can not go get empty packages.


As I said earlier, in my company we have no problems using private repositories, as long as we have those git configuration in place I mentioned earlier.

In our code it looks like this:

import "example.com/group/project/package"

It works like a charm.