简体   繁体   English

Go - 我是否为所有项目都有一个工作区,或者每个项目有一个工作区?

[英]Go - Do I have one workspace for all projects, or one workspace per project?

When using Go how are projects organized? 使用Go如何组织项目? I'm a bit confused on the workspaces part. 我对工作区部分感到有点困惑。 I've had a read of the following: https://golang.org/doc/code.html#Workspaces ... and this part has thrown me off a little: 我已经阅读了以下内容: https//golang.org/doc/code.html#Workspaces ...这部分让我失望了:

A typical workspace contains many source repositories containing many packages and commands. 典型的工作空间包含许多包含许多包和命令的源存储库。 Most Go programmers keep all their Go source code and dependencies in a single workspace. 大多数Go程序员将他们所有的Go源代码和依赖项保存在一个工作区中。

Does this mean that for each project I create it is a seperate workspace? 这是否意味着我创建的每个项目都是一个单独的工作区? For example if two projects use the same package, I would have two copies of that package on my computer. 例如,如果两个项目使用相同的包,我将在我的计算机上有两个该包的副本。

Or, does it mean you have a main workspace and your projects share those packages? 或者,这是否意味着您有一个主工作区,您的项目共享这些包?

Bit confused. 有点困惑。

I personally like to have a different GOPATH per project. 我个人希望每个项目都有不同的GOPATH。 If you are fine using a tool to automate the process you can use vg which will take care of managing different GOPATHs for your projects. 如果您可以使用工具自动化该过程,则可以使用vg来管理项目的不同GOPATH。

The neat bit is that it integrates with most shells and auto-detect projects as you cd them. 整洁的一点是它与大多数shell集成并在你cd它们时自动检测项目。

You have one workspace and projects share the packages. 您有一个工作区,项目共享包。

It's there in the overview section : 它在概述部分

Go programmers typically keep all their Go code in a single workspace. 程序员通常会将所有Go代码保存在一个工作区中。

Note that this differs from other programming environments in which every project has a separate workspace and workspaces are closely tied to version control repositories. 请注意,这与其他编程环境不同,在这些环境中,每个项目都有一个单独的工作区,工作区与版本控制存储库紧密相关。

Edit : If you use vendoring , you can effectively get a separate workspace for each project. 编辑 :如果您使用vendoring ,您可以有效地为每个项目获得一个单独的工作区。 This brings things closer to how other programming languages work. 这使得事情更接近其他编程语言的工作方式。

So far, I use different workspaces either when I want to use a different version of Go or I want to separate my private work from the code the kids and I have fun with. 到目前为止,当我想使用不同版本的Go或者我想将我的私人作品与孩子和我喜欢的代码分开时,我使用不同的工作区。 Also if I want to play with some open source code but want a simple way of cleaning it all up later. 此外,如果我想玩一些开源代码,但想要一个简单的方法来清理它。

Something like 就像是

mk /tmp/tmpgo
cd /tmp/tmpgo
# Copy or edit a setenv file
. setenv  # I use bash

The setenv file looks something like this. setenv文件看起来像这样。

export GOROOT=$HOME/go16
export GOPATH=$PWD
export GOBIN=$GOPATH/bin
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
export PS1='\[\033[01;32m\]workspacenamehere\[\033[01;33m\] \W\[\033[00m\] '

This gives me a go workspace with its own bin, src, pkg subdirectories. 这给了我一个go工作区,它有自己的bin,src,pkg子目录。 I can go get anything I want. 我可以得到任何我想要的东西。 Later I can delete the whole temporary directory if I like. 如果我愿意,我可以删除整个临时目录。 Getting things from repositories like github.com has a tendency to get many packages from other contributors, but because it puts them all into a clean src subdirectory, it's easy to use find and see what has been pulled down. 从像github.com这样的存储库中获取东西的趋势是从其他贡献者那里获得许多包,但是因为它将它们全部放入一个干净的src子目录中,所以很容易使用find并查看已被删除的内容。 And later it is even easier to remove everything from the hd again. 后来再次从高清中删除所有内容变得更加容易。

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

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