有人可以解释为什么GOPATH很方便,应该如何使用它? [英] Can someone explain why GOPATH is convenient and how it should be used in general?
问题描述
我是Go编程语言的新手,每个教程都从将GOPATH设置为当前项目文件夹开始。
我错过了什么吗?程序员是否真的应该在他的 cd
到他的新Go项目文件夹时手动设置GOPATH?我已经阅读了关于GOPATH的几个常见问题解答条目,但仍然无法将其包围。
为什么GOROOT存在呢?它的目的是什么?
是否有自动工具检测当前目录是否为Go项目的根文件夹(例如,通过某个隐藏文件)并自动将GOPATH更改为此目录?
谢谢,任何建议真的appriciated
ps。例如,我开发完全脱节的Go项目A,B和C,他们是否应该生活在单一的工作空间环境中?我想不是,但是我应该怎么处理GOPATH和GOROOT? GOPATH
是将所有软件包集中到一个公共工作区中。它本身并不是一个真正的新概念(例如考虑java classpath),但是Go通过不支持包版本化的方式以非常简单的方式使用。
Go程序员不能在进入一个新的项目文件夹时手动设置 GOPATH
。每个项目文件夹本身应该是一个包,并且与其他包一起驻留在 GOPATH
中,所以 GOPATH
应该只能设置一次。教程首先设置 GOPATH
,以便将教程工作区与其他任何区隔开。
$ b $ p
GOROOT
设置为向Go程序员提供标准软件包,您无需对其执行任何操作。简而言之, GOROOT
有一条规则:永远不要碰它。不要在其中安装任何东西,不要修改标准软件包等。
我没有意识到在当前目录中检测项目的工具,但创建应该不是很复杂。
您如何处理不同项目取决于您。这样做的方法是将每个项目作为一个包放入 $ GOPATH / src
目录中,并从那里完成所有工作。因为我不太喜欢它,所以我将 GOPATH
定义为 $ HOME / .go
。然后,我将每个项目放在其他地方(我的计算机中的任何地方)的专用目录中,并将项目目录符号链接到我的 $ GOPATH / src
dir中。然后,我可以使用每个命令编译为无,将项目作为另一个的包使用等。
I am new to Go programming language and every tutorial starts off from setting GOPATH to current project folder.
Am I missing something? Is programmer really supposed to set GOPATH manually when he cd
to his new Go project folder? I have read several FAQ entries about GOPATH but still couldn't wrap my head around it.
And why does GOROOT exist then? What's its purpose?
Are there any automatic tools which detects if current directory is root folder of Go project (for example by some hidden file) and changes GOPATH to this directory automatically?
Thank you, any advice really appriciated
ps. For example I develop completely disjoint Go projects A, B and C, should they live in single "workspace" environment? I guess not, but what I should do with GOPATH and GOROOT then?
The goal of the GOPATH
is to centralize all packages into one common workspace. It is not really a new concept by itself (think of the java classpath for example), but Go use in a drastically simple way by not supporting packages versionning.
The Go programmer isn't suposed to set GOPATH
manually when entering a new project folder. Each project folder is supposed to be a package by itself, and reside in the GOPATH
along other packages, so GOPATH
should be set only once. Tutorials begin by setting the GOPATH
in order to isolate the tutorial workspace from anything other.
GOROOT
is set to provide the standard packages to the Go programmer, you don't need to do anything with it. To put it short, there is a single rule for GOROOT
: never, ever, touch it. Don't install anything in it, don't modify standard packages, etc.
I'm not aware of a tool to detect go projects in the current directory, but it shouldn't be highly complex to create.
How you handle differents projects is up to you. The go way to do it is to put every project as a package in the $GOPATH/src
dir and do everything from there. As I don't really like it, I defined my GOPATH
to be $HOME/.go
. Then I put each project in a dedicated directory somewhere else (anywhere in my computer), and symlink the project directory into my $GOPATH/src
dir. I can then use every command compile as nothing, use the project as package for another one, etc.
这篇关于有人可以解释为什么GOPATH很方便,应该如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!