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

**URL:** <https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896>\
**Category:** Technical Discussion\
**Created:** [March 12, 2020, 3:10am UTC](https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896 "2020-03-12T03:10:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwschulze](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dwschulze/32/7228_2.png) [@dwschulze](https://forum.golangbridge.org/u/dwschulze)\
**Post date:** [March 12, 2020, 3:10am UTC](https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896/1 "2020-03-12T03:10:09Z")

</div>

Go recognizes some public servers like [github.com](http://github.com) and [bitbucket.org](http://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?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [March 12, 2020, 6:00am UTC](https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896/2 "2020-03-12T06:00:47Z")

</div>

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](http://example.com) with SSH access:

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

```

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

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.

---

<div class="post-metadata">

**Author:** ![dwschulze](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dwschulze/32/7228_2.png) [@dwschulze](https://forum.golangbridge.org/u/dwschulze)\
**Post date:** [March 12, 2020, 12:58pm UTC](https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896/3 "2020-03-12T12:58:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [March 12, 2020, 2:38pm UTC](https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896/4 "2020-03-12T14:38:48Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![dwschulze](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dwschulze/32/7228_2.png) [@dwschulze](https://forum.golangbridge.org/u/dwschulze)\
**Post date:** [March 12, 2020, 3:12pm UTC](https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896/5 "2020-03-12T15:12:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [March 12, 2020, 3:22pm UTC](https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896/6 "2020-03-12T15:22:03Z")

</div>

> [@dwschulze](#):
>
> go get thinks the server name is part of a package name or path

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

> [@dwschulze](#):
>
> All the .go files in one of those subdirectories belong to the same package. That’s how go packages have always worked.

Yes, thats true and I won’t object.

> [@dwschulze](#):
>
> Just about every git project I’ve written has a src/ directory under which are other directories containing .go files.

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:

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

```

It works like a charm.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [June 10, 2020, 3:22pm UTC](https://forum.golangbridge.org/t/using-go-get-to-retrieve-modules-packages-from-a-private-server/17896/7 "2020-06-10T15:22:04Z")

</div>

Closed on June 10, 2020, 3:22pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
