Go工作区与自定义文件夹结构

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

Go workspace vs custom folder structure

问题

我目前正在学习Go语言,并且了解到Go建议(期望?强制?)使用特定的文件夹布局来组织工作空间:

  • my-go-workspace
    • src
    • bin
    • pkg

我完全理解这样做的原因,并且确实看到了它的优势。所以,基本上,我认为这是一件好事。

唯一让我不喜欢的是,这与我的其他项目完全不同的结构。例如,对于我的Node.js项目,有这样一个文件夹结构:

  • projects
    • moduleA
    • moduleB
    • ...

现在,如果我想要将Node.js模块和Go包放在一起,使用统一的文件夹结构,我要么必须同时使用Go的方式来组织Node.js项目(感觉不对),要么反过来(同样感觉不对)。

我猜这可能是一个愚蠢的想法,我应该只遵循Go的建议,并接受两种不同的代码组织方式(嘿,这是不同的语言,为什么文件夹布局要相同呢?),但是在某种程度上,我觉得这不够干净。

我知道这是一个高度主观和个人的问题,但我的问题是,是否有任何解决这个问题的实践方法?或者我应该只是接受现状呢?

英文:

I'm currently learning Go, and I have learned that Go suggests (expects? enforces?) that you use a specific folder layout for your workspace:

- my-go-workspace
  - src
  - bin
  - pkg

I perfectly understand the reasoning for this, and I definitely see its advantages. So, basically, I think it's a good thing to have a structure like this.

The only thing I don't like about this, is that it is a completely different structure from my other projects. E.g., for my Node.js projects there is a folder such as this:

- projects
  - moduleA
  - moduleB
  - ...

Now if I want to keep Node.js modules and Go packages side-by-side, in a unified folder structure I either have to use Go's way for Node.js as well (which feels wrong), or vice-versa (which feels wrong either).

I guess it's probably a dumb idea, and I should simply stick with Go's suggestion and live with two different ways of organizing my code (hey, it's different languages, why should the folder layout be the same?), but somehow this does not feel clean to me.

I know that this a highly subjective and personal, but my question is, if there are any practices out there on how to solve this? Or should I just live with it?

答案1

得分: 4

一个更简单的想法:

golos-projects
 L node-project-a
    L what
    L ever
    L yolo
 L node-project-b
    L we
    L love
    L npm
 L go-project-a
    L src
    L pkg
    L bin
 L go-project-b
    L src
    L pkg
    L bin

只需将你的GOPATH设置为例如golos-projects/go-project-b,然后开始编码。当你想切换到另一个项目时,只需将你的GOPATH更改为新项目的文件夹。

英文:

A more simple idea:

golos-projects
 L node-project-a
    L what
    L ever
    L yolo
 L node-project-b
    L we
    L love
    L npm
 L go-project-a
    L src
    L pkg
    L bin
 L go-project-b
    L src
    L pkg
    L bin

And just set your GOPATH to e.g. golos-projects/go-project-b and hack away. When you want to switch to another project just change your GOPATH to the new project's folder.

答案2

得分: 3

你可以拥有你想要的结构,只要$GOPATH引用的工作区遵循以下约定:

my-go-workspace
  - src
  - bin
  - pkg

在你的情况下,src/可以简单地是一个指向projects/的符号链接(可以位于任何其他位置),在其中你可以有Node和Go项目。

英文:

You can have the structure you want, as long as $GOPATH references a workspace which follows the convention

my-go-workspace
  - src
  - bin
  - pkg

In your case, src/ could simply be a symlink to projects/ (located anywhere else), in which you would have Node and Go projects.

huangapple
  • 本文由 发表于 2014年11月28日 16:16:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/27184486.html
匿名

发表评论

匿名网友

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

确定