-2

I have problems setting up my first go project. I want to keep my packages out of my git repository.

go get installs my packages by default in my /src folder. This way I can't simply ignore a folder to ignore all packages.

Can I install all my packages in for example /pkg and how would I do this? Is there a Go way of solving this issue?

In large open source projects written in Go like for example Kubernetes I cannot find a /src directory.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
thiloilg
  • 1,733
  • 2
  • 18
  • 23

1 Answers1

2

This sounds like an issue in your workspace.

Your workspace directory should look like this, but your workspace should not be the root of your repo.

/bin //<--compiled binaries
/src
   /adomain.com/yourstuff //<--these folders are where your repo root should be
   /gihub.com/otherstuff //<--these folders are your dependencies
/pkg //<-- installed packages / program files

Read the official Go documentation here:

https://golang.org/doc/code.html

A widely-used standard for organizing projects is below:

https://github.com/golang-standards/project-layout

See also: https://golang.org/doc/effective_go.html

VolatileCoder
  • 244
  • 1
  • 8
  • Is it common practise to add the installed packages in the git repository? – thiloilg Sep 17 '19 at 17:00
  • 1
    No. Go get will auto-download dependencies (in theory). If you follow the steps in the first link above, everything works well. Your dependencies should be fully-qualified, eg. "github.com/user/project" – VolatileCoder Sep 17 '19 at 17:01
  • Do you know why for example open source projects like Kubernetes do not have a src folder in their repository? – thiloilg Sep 17 '19 at 17:14
  • 1
    @VolatileCoder: You references appear to be obsolete. They do not appear to have been updates for Go modules. – peterSO Sep 17 '19 at 17:27
  • 1
    Without modules, it _is_ common to store dependencies in the git repository, in the `/vendor` directory. Although this isn't required, or always desired. – Jonathan Hall Sep 17 '19 at 17:29
  • 1
    @thiloilg the `src` directory *is not your project*. It is a workspace containing all your non-module projects and their dependencies. If you're checking out directly into `src`, you haven't read the linked guides. On the other hand, if you are using modules, any downloaded dependencies are in a cache directory, also outside your project directory. Modules also largely obviates the need for vendoring dependencies since it specifies versions and hashes; I personally have not found it at all common to see a vendor directory in a module project. – Adrian Sep 17 '19 at 17:53
  • @Adrian Thanks I understand. I have a mono repo with a couple of node microservices so i can't use src as my workspace. I guess modules is the way to go then. – thiloilg Sep 17 '19 at 17:58
  • 1
    Sure you can, you just have to put your project in a reasonable place, again according to the linked documentation. `src/myscm.co/myproject/mypackage` can contain NodeJS code, it will just be ignored. – Adrian Sep 17 '19 at 17:59