我可以有多个GOPATH目录吗?

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

Can I have multiple GOPATH directories?

问题

我将我的GOPATH设置为:

/Users/me/dev/go

我有以下目录:

/Users/me/dev/go/src/client1
/Users/me/dev/go/src/client2
/Users/me/dev/go/src/client3

还有:

/Users/me/dev/client1/rails_project
/Users/me/dev/client2/php_project
等等。

我不喜欢在我的根开发文件夹中被迫拥有这个通用的"go"目录,它包含了许多不同客户的go项目。

英文:

I set my GOPATH to

/Users/me/dev/go

and I have

/Users/me/dev/go/src/client1
/Users/me/dev/go/src/client2
/Users/me/dev/go/src/client3

and also

/Users/me/dev/client1/rails_project
/Users/me/dev/client2/php_project
etc.

I don't like how in my root dev folder I'm forced to have this general "go" dir that holds many different client's go projects.

答案1

得分: 84

是的,GOPATH 是一个目录列表(类似于 PATH)。运行 go help gopath 查看详细信息。例如,在 Linux 上,我有以下设置:

$ go env
GOROOT="/home/peter/go"
GOPATH="/home/peter/gopath:/home/peter/public/gopath"
$

在 Windows 上也有类似的设置。

注意:Linux 使用 : 作为 GOPATH 列表分隔符;Windows 使用 ; 作为分隔符。

如果你使用 go get,它会默认使用列表中的第一个目录。

运行 go env 来检查是否一切设置正确。

英文:

Yes, GOPATH is a list of directories (like PATH). Run go help gopath for details. For example, on Linux, I have:

$ go env
GOROOT="/home/peter/go"
GOPATH="/home/peter/gopath:/home/peter/public/gopath"
$

I have something similar on Windows.

Note: Linux uses : as the GOPATH list separator; Windows uses ; as the separator.

If you use go get it will default to the first directory in the list.

Run go env to check that everything is correct.

答案2

得分: 27

这篇博客文章提供了关于如何以及为什么要在GOPATH中设置多个值的非常好的解释,特别是当它说:

我的GOPATH由3个文件夹或GOPATH工作空间组成。

第一个是我的landing workspace。由于它排在第一位,每当我使用go get获取新的包时,它总是最先放在这个工作空间中。

Go会在GOPATH中的每个目录中搜索源代码,但新的包总是下载到列表中的第一个目录中。
我规定从不在这里进行任何开发,所以每当这个文件夹变得太大(有一些我不使用的Go包)时,清理它总是完全安全的。毕竟,它只包含我可以通过go get再次获取的Go包。

我的第二个工作空间是用于存放我个人的Go包和其他我可能想要“收藏”或进行一些开发的包。我将我经常使用的东西从第一个工作空间移动到第二个工作空间。

我的第三个工作空间专门用于我工作中的私有Go包及其依赖项。将我的工作包与我个人的东西分开是很方便的,这样它们就不会相互干扰。

英文:

This blog post gives a very nice explanation about how and why one would set multiple values in GOPATH, especially when it says:

> My GOPATH consists of 3 folders or GOPATH workspaces.
>
> The first one is my landing workspace. Since it's listed first, whenever I go get any new package, it always ends up in this workspace.
>
> Go searches each directory listed in GOPATH to find source code, but new packages are always downloaded into the first directory in the list.
I make it a rule to never do any development in there, so it's always completely safe to clean this folder whenever it gets too large (with Go packages I don't use). After all, it only has Go packages that I can get again with go get.
>
> My second workspace is for all my personal Go packages and any other packages I may want to "favorite" or do some development on. I move things I use regularly from first workspace into second.
>
> My third workspace is dedicated to the private Go packages from my work, and their dependencies. It's convenient to have my work packages separate from all my personal stuff, so they don't get in each other's way.

答案3

得分: 17

是的。

引用Go自身的话:

$ go help gopath

Go路径用于解析导入语句。
它由go/build包实现并记录在文档中。

GOPATH环境变量列出了查找Go代码的位置。
在Unix上,该值是一个以冒号分隔的字符串。
在Windows上,该值是一个以分号分隔的字符串。
在Plan 9上,该值是一个列表。

必须设置GOPATH以在标准Go树之外获取、构建和安装软件包。

GOPATH中列出的每个目录都必须具有规定的结构:

src目录保存源代码。src目录下的路径决定了导入路径或可执行文件的名称。

...

英文:

Yes.

To cite Go itself:

$ go help gopath

> The Go path is used to resolve import statements.
> It is implemented by and documented in the go/build package.
>
> The GOPATH environment variable lists places to look for Go code.<br/>
> On Unix, the value is a colon-separated string.<br/>
> On Windows, the value is a semicolon-separated string.<br/>
> On Plan 9, the value is a list.
>
> GOPATH must be set to get, build and install packages outside the
> standard Go tree.
>
> Each directory listed in GOPATH must have a prescribed structure:
>
> The src directory holds source code. The path below src
> determines the import path or executable name.
>
> ...

huangapple
  • 本文由 发表于 2016年3月16日 01:07:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/36017724.html
匿名

发表评论

匿名网友

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

确定