Go – Do I have one workspace for all projects, or one workspace per project?

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

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

问题

在使用Go语言时,项目是如何组织的?我对工作区部分有点困惑。我已经阅读了以下内容:https://golang.org/doc/code.html#Workspaces ... 这部分让我有点困惑:

一个典型的工作区包含许多包含多个包和命令的源代码仓库。大多数Go程序员将所有的Go源代码和依赖项都保存在一个单独的工作区中。

这是否意味着对于每个项目我创建一个单独的工作区?例如,如果两个项目使用相同的包,那么我在计算机上会有两个包的副本。

或者,它是指你有一个主工作区,你的项目共享这些包?

有点困惑。

英文:

When using Go how are projects organized? 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:

> 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.

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.

答案1

得分: 7

我个人喜欢每个项目都有一个不同的GOPATH。如果你愿意使用一个工具来自动化这个过程,你可以使用vg,它会负责管理你项目的不同GOPATH。

最棒的是,它可以与大多数shell集成,并在你使用cd命令切换项目时自动检测。

英文:

I personally like to have a different GOPATH per project. 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.

The neat bit is that it integrates with most shells and auto-detect projects as you cd them.

答案2

得分: 6

到目前为止,我在使用不同的工作空间时,要么是因为我想使用不同版本的Go,要么是因为我想将我的私人工作与我和孩子们一起玩的代码分开。此外,如果我想玩一些开源代码,但又想简单地清理掉所有东西,我也会使用不同的工作空间。

类似于这样:

mk /tmp/tmpgo
cd /tmp/tmpgo
# 复制或编辑一个 setenv 文件
. setenv  # 我使用 bash

setenv 文件的内容大致如下:

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

这样就给我提供了一个带有自己的 bin、src、pkg 子目录的 Go 工作空间。我可以随心所欲地获取任何我想要的东西。稍后,如果我愿意,我可以删除整个临时目录。从像 github.com 这样的代码库获取东西往往会从其他贡献者那里获取许多包,但由于它们都被放在一个干净的 src 子目录中,所以很容易使用 find 命令查看已经下载的内容。而且稍后再次从硬盘上删除所有东西也更加容易。

英文:

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. 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.

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

This gives me a go workspace with its own bin, src, pkg subdirectories. 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. And later it is even easier to remove everything from the hd again.

答案3

得分: 4

你有一个工作区,项目共享包。

概述部分有说明:

Go程序员通常将所有的Go代码放在一个单独的工作区中。

需要注意的是,这与其他编程环境不同,其他编程环境中每个项目都有一个单独的工作区,并且工作区与版本控制仓库密切相关。

编辑:如果你使用vendoring,你可以为每个项目有效地获得一个单独的工作区。这使得Go更接近其他编程语言的工作方式。

英文:

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.
>
> 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. This brings things closer to how other programming languages work.

huangapple
  • 本文由 发表于 2016年3月24日 21:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/36200806.html
匿名

发表评论

匿名网友

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

确定