5

I have a pretty simple Go project that I'm trying to restructure so that it follows the normal Go project structure (and so I can run go build).

I currently have two source files, both with package main. All the files, including a few text configuration files that my program needs at runtime.

So right now, it looks like:

<project_name>
    - main.go
    - source2.go
    - config_file.txt

I can run go build when I'm in this directory, and it creates one binary (named <project_name>. This works fine, but I'd like to set this up to better follow the Go standard package structure (specifically so that Intellij IDEA will recognize it as a valid project).

Right now, I have the entire <project_name> directory in Git, and I'd like to keep it that way.

I tried putting the source files in a folder called src, but then go build says there aren't any source files to compile.

How should I structure this?

EDIT:

Figured out the issue with putting stuff in src/: I need to run go build <project_name>.
I'm still wondering if there's a way to set up a project without a global GOPATH. I have all my projects under one folder, with a subfolder for each project (not all the projects are Go project). I'd like to keep that system.

What I want is:

projects/
    - project 1/
         - src/
         - bin/
         - pkg/
    - project 2/
         - src/
         - bin/
         - pkg/

Then I'd like to be able to run go build <project_name> (while I'm in that project's directory) and have it compile that project. Is that possible?

Isaac Dontje Lindell
  • 3,246
  • 6
  • 24
  • 35
  • 1
    possible duplicate of [Organizing a multiple-file Go project](http://stackoverflow.com/questions/9985559/organizing-a-multiple-file-go-project) – Kavu Feb 13 '14 at 18:47
  • Note that you can always just specify your `GOPATH` as a directory where non-Go projects might also exist. Go will just ignore them. – elithrar Feb 14 '14 at 01:30
  • Right, but I don't want to have to have a common src folder for even all my Go projects. – Isaac Dontje Lindell Feb 14 '14 at 01:34
  • That's normal though: src// is how `go get` operates. Don't think of the `src` folder as containing a mess of code: it's just a collection of package source code (as opposed to binaries or headers). My GOPATH contains a bunch of `github.com/user/` folders for different packages I've downloaded, a bunch of `code.google.com/project` folders for other packages I've downloaded and of course my own projects. I make this a tad easier by symlinking my own projects into somewhere more accessible without having to mess with my GOPATH. – elithrar Feb 14 '14 at 01:44

1 Answers1

6

The "canonical" way to organize your Go code is described in How to Write Go Code. It is explained in a less formal way in this blog post. This concept is kind of contrary to what you have in mind - you have a workspace directory, specified by the GOPATH environment variable, and the source code of all projects resides in subdirectories of the "src" directory of the workspace. You can have multiple workspaces if you specify several directories in GOPATH.

I would suggest you give the "recommended" way to organize your code a chance, maybe it will grow on you. It may seem a bit unusual, but it has its advantages. If you find out you absolutely can't live with it, you can still work around it, e.g. by setting GOPATH dinamically in a script.

As for the Golang IDEA plugin, the last version I tried some time ago didn't yet support this project structure, but newer versions may have changed that. In fact, one of the plugin's authors (dlsniper) has added a comment to the above blog post giving examples of alternative project structures that still use a global GOPATH.

rob74
  • 4,939
  • 29
  • 31
  • I ended up putting ALL my development projects (Go or not) in the GOPATH. The folder/file structure has been very accommodating and all my git repositories sit in one place, with one unified structure now. It's nice. – Matt Feb 14 '14 at 03:09