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?
For command-line applications, my original article suggested placing the main.go file in the top-level directory of the repository. This recommendation made installing the project easier with go install (formerly go get ).
Go is an open source project developed by a team at Google and many contributors from the open source community. Go is distributed under a BSD-style license.
/cmd. This folder contains the main application entry point files for the project, with the directory name matching the name for the binary. So for example cmd/simple-service meaning that the binary we publish will be simple-service .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With