简体   繁体   English

Go 工作区的最佳实践是什么?

[英]Whats a good best practice with Go workspaces?

I'm just getting into learning Go, and reading through existing code to learn "how others are doing it".我刚刚开始学习 Go,并阅读现有代码以了解“其他人是如何做的”。 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.在这样做时,go“工作区”的使用,尤其是当它与项目的依赖项相关时,似乎无处不在。

What (or is there) a common best practice around using a single or multiple Go workspaces (ie definitions of $GOPATH) while working on various Go projects?在处理各种 Go 项目时,围绕使用单个或多个 Go 工作区(即 $GOPATH 的定义)有什么(或有)常见的最佳实践? 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)?我应该期望有一个单一的 Go 工作区,它有点像我所有项目的中央代码存储库,还是在我开始处理这些项目中的每一个时明确地分解它并设置 $GOPATH(有点像 python虚拟环境)?

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.我认为每个项目有一个$GOPATH更容易,这样你就可以为不同的项目使用相同包的不同版本,并根据需要更新包。

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).使用中央存储库,很难更新包,因为这样做可能会破坏不相关的项目(如果包更新有重大更改或新错误)。

I used to use multiple GOPATHs -- dozens, in fact.我曾经使用过多个 GOPATH——实际上是几十个。 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.我现在只有一个GOPATH 并且我实际上把我所有的开发项目——不管去不去——都放在里面。 With one central workspace, I can still keep each project in its own git repository ( src/<whatever> ) and use git branching to manage dependencies when necessary (in practice, very seldom).有了一个中央工作区,我仍然可以将每个项目保存在自己的 git 存储库 ( src/<whatever> ) 中,并在必要时使用 git 分支来管理依赖项(实际上很少使用)。

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).我的建议:只使用一个或两个工作区(例如,如果您需要将工作和个人代码更加分开,尽管推荐的包路径命名约定应该为您做到这一点)。

If you just set GOPATH to $HOME/go or similar and start working, everything works out of the box and is really easy.如果您只是将GOPATH设置为$HOME/go或类似的并开始工作,那么一切都是开箱即用的,非常简单。

If you make lots of GOPATH s 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.如果您为许多项目制作了许多带有许多 bin 目录的GOPATH s,这些项目在各种新鲜状态下都有许多共同的依赖项,这应该很明显,这会让自己变得更难。 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.如果你发现,有时你需要隔离一些东西,那么你可以创建一个单独的GOPATH来处理这种情况。

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.在过去的四年里,我已经积累了近 100 个项目。 I almost always work in GOPATH , which is $HOME/go on my computers.我几乎总是在GOPATH工作,它是我电脑上的$HOME/go

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.在您的所有项目中使用一个 GOPATH 非常方便,但我发现这仅适用于我自己的个人项目。

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.我为我维护的每个生产系统使用单独的 GOPATH,因为我在每个 GOPATH 的目录树中使用 git 子模块来冻结依赖项。

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.通过将 GOPATH 设置为 ~/code/my-project,然后它在该项目中使用依赖项一和依赖项二的 git 子模块,而不是使用全局依赖项。

Try envirius (universal virtual environments manager) .尝试envirius(通用虚拟环境管理器) It allows to compile any version of go and create any number of environments based on it.它允许编译任何版本的go并基于它创建任意数量的环境。 $GOPATH / $GOROOT are depend on each particular environment. $GOPATH / $GOROOT取决于每个特定的环境。

Moreover, it allows to create environments with mixed languages (for example, python & go in one environment).此外,它允许使用混合语言创建环境(例如, pythongo在一种环境中)。

At my company I created Virtualgo to make managing multiple GOPATH s super easy.在我的公司,我创建了Virtualgo以使管理多个GOPATH变得非常容易。 A couple of advantages over handling it manually are:与手动处理相比,它有几个优点:

  • Automatic switching to the correct GOPATH when you cd to a project. cd到项目时自动切换到正确的GOPATH
  • It integrates well with vendoring tools它与销售工具很好地集成
  • It also sets the new GOBIN in your path, so you can use the executables installed there.它还在您的路径中设置新的 GOBIN,因此您可以使用安装在那里的可执行文件。
  • It still has your original GOPATH as a backup.它仍然有你原来的GOPATH作为备份。 If a package is not found in the project specific workspace it will search the main GOPATH .如果在项目特定的工作区中找不到包,它将搜索主GOPATH

对我来说,一个工作区 + Godep是最好的。

I follow KISS - one GOPATH, two go paths:我遵循 KISS - 一个 GOPATH,两条路径:

export GOPATH=$HOME/go:$HOME/development/go出口 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.这样第三方的东西放在一个中心位置(包安装默认使用第一个路径条目),我可以灵活地将我的项目移动到其他地方,在第二个路径条目。

You might want to try the direnv package.您可能想尝试使用direnv包。

https://direnv.net/ https://direnv.net/

Just use GoSwitch.只需使用 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.它将使该项目目录成为您的 gopath,并将该项目的确切 bin 文件夹添加/删除到路径。 https://github.com/buffonomics/goswitch https://github.com/buffonomics/goswitch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM