关于共享GOPATH的良好实践是什么?

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

What's a good practice regarding sharing the GOPATH?

问题

我刚开始学习Go语言,并通过阅读现有的代码来学习“别人是如何做的”。在这个过程中,关于使用Go的“工作区”特别是与项目依赖相关的问题似乎存在很多不同的做法。

在处理各种Go项目时,是否存在一种常见的最佳实践,即使用单个或多个Go工作区(即$GOPATH的定义)?我应该期望拥有一个单一的Go工作区,它类似于我所有项目的代码中央仓库,还是应该明确地将其分开,并在处理每个项目时设置$GOPATH(类似于Python的虚拟环境)?

英文:

I'm just getting into learning Go, and reading through existing code to learn "how others are doing it". In doing so, the use of a go "workspace", especially as it relates to a project's dependencies, seems to be all over the place.

What (or is there) a common best practice around using a single or multiple Go workspaces (i.e. definitions of $GOPATH) while working on various Go projects? Should I be expecting to have a single Go workspace that's sort of like a central repository of code for all my projects, or explicitly break it up and set up $GOPATH as I go to work on each of these projects (kind of like a python virtualenv)?

答案1

得分: 33

我认为每个项目都有一个独立的$GOPATH更容易,这样你可以为不同的项目使用不同版本的相同包,并根据需要更新包。

使用一个中央仓库,更新一个包可能会很困难,因为这样做可能会破坏其他不相关的项目(如果包的更新有破坏性的改变或新的错误)。

英文:

I think it's easier to have one $GOPATH per project, that way you can have different versions of the same package for different projects, and update the packages as needed.

With a central repository, it's difficult to update a package as you might break an unrelated project when doing so (if the package update has breaking changes or new bugs).

答案2

得分: 32

我过去使用多个GOPATH,事实上有几十个。在不同项目之间切换和维护依赖关系变得更加困难,因为在一个工作空间中引入有用的更新需要在其他工作空间中进行,有时我会忘记,然后疑惑为什么某个依赖在一个项目中可以工作但在另一个项目中却不行。真是一场灾难。

现在我只有一个GOPATH,实际上我把所有的开发项目(无论是Go还是其他语言)都放在其中。有了一个中心化的工作空间,我仍然可以将每个项目放在自己的git仓库中(src/<whatever>),并在必要时使用git分支来管理依赖关系(实际上很少需要)。

我的建议是:只使用一个工作空间,或者最多两个(比如如果你需要更好地区分工作和个人代码,尽管推荐的包路径命名规范应该已经为你做到了这一点)。

英文:

I used to use multiple GOPATHs -- dozens, in fact. Switching between projects and maintaining the dependencies was a lot harder, because pulling in a useful update in one workspace required that I do it in the others, and sometimes I'd forget, and scratch my head, wondering why that dependency works in one project but not another. Fiasco.

I now have just one GOPATH and I actually put all my dev projects - Go or not - within it. With one central workspace, I can still keep each project in its own git repository (src/&lt;whatever&gt;) and use git branching to manage dependencies when necessary (in practice, very seldom).

My recommendation: use just one workspace, or maybe two (like if you need to keep, for example, work and personal code more separate, though the recommended package path naming convention should do that for you).

答案3

得分: 7

如果你只是将GOPATH设置为$HOME/go或类似的路径并开始工作,一切都可以直接使用,非常简单。

如果你创建了很多GOPATH,每个GOPATH都有很多bin目录,用于很多项目,这些项目有很多共同的依赖项,而且这些依赖项的状态各不相同,那么很明显,你会让事情变得更加困难。这只会增加更多的工作量。

如果你发现有时需要隔离一些东西,那么你可以创建一个单独的GOPATH来处理这种情况。

但是一般来说,如果你发现自己做了更多的工作,往往是因为你选择让事情变得更加困难。

在过去的四年里,我积累了接近100个Go项目。我几乎总是在GOPATH中工作,我的计算机上的GOPATH路径是$HOME/go

英文:

If you just set GOPATH to $HOME/go or similar and start working, everything works out of the box and is really easy.

If you make lots of GOPATHs with lots of bin dirs for lots of projects with lots of common dependencies in various states of freshness you are, as should be quite obvious, making things harder on yourself. That's just more work.

If you find that, on occasion, you need to isolate some things, then you can make a separate GOPATH to handle that situation.

But in general, if you find yourself doing more work, it's often because you're choosing to make things harder.

I've got what must be approaching 100 projects I've accumulated in the last four years of go. I almost always work in GOPATH, which is $HOME/go on my computers.

答案4

得分: 5

在所有项目中使用一个统一的GOPATH非常方便,但我发现这只适用于我个人的项目。

对于我维护的每个生产系统,我使用单独的GOPATH,因为我在每个GOPATH的目录树中使用git子模块来冻结依赖关系。

所以,像这样:

~/code/my-project
- src
  - github.com
    + dependency-one
    + dependency-two
    - my-org
      - my-project
        * main.go
        + package-one
        + package-two
- pkg
- bin

通过将GOPATH设置为~/code/my-project,它将使用该项目中的dependency-one和dependency-two git子模块,而不是使用全局依赖项。

英文:

Using one GOPATH across all of your projects is very handy, but I find this to only be the case for my own personal projects.

I use a separate GOPATH for each production system I maintain because I use git submodules in each GOPATH's directory tree in order to freeze dependencies.

So, something like:

~/code/my-project
- src
  - github.com
    + dependency-one
    + dependency-two
    - my-org
      - my-project
        * main.go
        + package-one
        + package-two
- pkg
- bin

By setting GOPATH to ~/code/my-project, then it uses the dependency-one and dependency-two git submodules within that project instead of using global dependencies.

答案5

得分: 5

你可以尝试使用envirius(通用虚拟环境管理器)。它允许编译任何版本的go并基于它创建任意数量的环境。$GOPATH/$GOROOT取决于每个特定的环境。

此外,它还允许在一个环境中创建混合语言的环境(例如,在一个环境中同时使用pythongo)。

英文:

Try envirius (universal virtual environments manager). It allows to compile any version of go and create any number of environments based on it. $GOPATH/$GOROOT are depend on each particular environment.

Moreover, it allows to create environments with mixed languages (for example, python & go in one environment).

1: https://github.com/ekalinin/envirius "envirius (universal virtual environments manager)"

答案6

得分: 5

在我的公司,我创建了Virtualgo来简化管理多个GOPATH的过程。与手动处理相比,它具有以下几个优点:

  • 当你切换到一个项目时,自动切换到正确的GOPATH
  • 它与供应商工具很好地集成在一起。
  • 它还将新的GOBIN设置到你的路径中,这样你就可以使用安装在那里的可执行文件。
  • 它仍然将你原来的GOPATH作为备份。如果在项目特定的工作区中找不到一个包,它将在主GOPATH中进行搜索。
英文:

At my company I created Virtualgo to make managing multiple GOPATHs super easy. A couple of advantages over handling it manually are:

  • Automatic switching to the correct GOPATH when you cd to a project.
  • It integrates well with vendoring tools
  • It also sets the new GOBIN in your path, so you can use the executables installed there.
  • It still has your original GOPATH as a backup. If a package is not found in the project specific workspace it will search the main GOPATH.

答案7

得分: 2

一个工作空间 + godep 对我来说是最好的选择。

英文:

One workspace + godep is best as for me.

答案8

得分: 1

我遵循KISS原则 - 一个GOPATH,两个go路径:

export GOPATH=$HOME/go:$HOME/development/go

这样第三方的东西就会放在一个中心位置(默认情况下,包安装使用第一个路径条目),而我可以灵活地将我的项目移动到第二个路径条目的其他位置。

英文:

I follow KISS - one GOPATH, two go paths:

export GOPATH=$HOME/go:$HOME/development/go

That way third party stuff goes in a central place (package install uses the first path entry by default), and I can flexibly move my projects elsewhere, at the second path entry.

答案9

得分: 0

你可能想尝试使用direnv软件包。

https://direnv.net/

英文:

You might want to try the direnv package.

https://direnv.net/

答案10

得分: -1

只需使用GoSwitch。它可以节省大量时间和精力。
将脚本添加到每个项目的根目录并进行源代码处理。
它将使该项目目录成为您的gopath,并将该项目的确切bin文件夹添加/删除到路径中。
https://github.com/buffonomics/goswitch

英文:

Just use GoSwitch. Saves a heck of a lot of time and sanity.
Add the script to the root of each of your projects and source it.
It will make that project dir your gopath and also add/removes the exact bin folder of that project to path.
https://github.com/buffonomics/goswitch

huangapple
  • 本文由 发表于 2013年12月22日 03:06:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/20722502.html
匿名

发表评论

匿名网友

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

确定