How should I structure a simple Go project?

huangapple go评论70阅读模式
英文:

How should I structure a simple Go project?

问题

我有一个相当简单的Go项目,我想重新组织它,使其遵循正常的Go项目结构(这样我就可以运行go build)。

我目前有两个源文件,都是package main。所有文件,包括一些程序在运行时需要的文本配置文件。

所以现在的结构是这样的:

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

当我在这个目录中运行go build时,它会创建一个二进制文件(命名为<project_name>)。这个工作正常,但我想将其设置为更好地遵循Go标准的包结构(特别是为了让Intellij IDEA将其识别为一个有效的项目)。

目前,我将整个<project_name>目录放在Git中,并希望保持这种方式。

我尝试将源文件放在一个名为src的文件夹中,但是go build会说没有要编译的源文件。

我应该如何组织这个项目的结构?

编辑:

找到了将东西放在src/中的问题:我需要运行go build <project_name>
我仍然想知道是否有一种方法可以设置一个没有全局GOPATH的项目。我将所有的项目都放在一个文件夹下,每个项目都有一个子文件夹(并非所有项目都是Go项目)。我想保持这个系统。

我想要的是:

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

然后,我想能够在项目目录中运行go build <project_name>,并编译该项目。这种方式可行吗?

英文:

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:

&lt;project_name&gt;
    - main.go
    - source2.go
    - config_file.txt

I can run go build when I'm in this directory, and it creates one binary (named &lt;project_name&gt;. 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&gt; 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 &lt;project_name&gt;.
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 &lt;project_name&gt; (while I'm in that project's directory) and have it compile that project. Is that possible?

答案1

得分: 6

Go语言中组织代码的“规范”方式在如何编写Go代码中有详细描述。在这篇博客文章中以一种不太正式的方式解释了这个概念。这个概念与你心中的想法有些相反 - 你有一个工作空间目录,由GOPATH环境变量指定,所有项目的源代码都驻留在工作空间的“src”目录的子目录中。如果在GOPATH中指定了多个目录,你可以拥有多个工作空间。

我建议你给“推荐”的代码组织方式一个机会,也许你会逐渐接受它。它可能看起来有些不寻常,但它有其优势。如果你发现你绝对无法接受它,你仍然可以通过在脚本中动态设置GOPATH来解决这个问题。

至于Golang IDEA插件,我之前尝试的最后一个版本还不支持这种项目结构,但更新的版本可能已经改变了。事实上,插件的其中一位作者(dlsniper)在上述博客文章中添加了一条评论,给出了仍然使用全局GOPATH的替代项目结构的示例。

英文:

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.

huangapple
  • 本文由 发表于 2014年2月14日 02:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/21762865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定