英文:
What is the programming paradigm behind the $GOPATH, what does it do?
问题
作为对Golang新手,我对$GOPATH
的概念感到有些困惑。
根据我的经验,它让我想起了Microsoft Windows机器上的“系统文件夹路径”(C:\Windows
,C:\Program Files
)- 它在概念上与此有关吗?
我看到了Go团队的描述,但它太实际了,只是解释了它是什么,而没有解释为什么。
那么,它为什么存在呢?
英文:
As someone new to Golang, I'm somewhat perplexed by the notion of the $GOPATH
.
The only thing in my experience that it reminds me of is the "System Folder Path" (C:\Windows
, C:\Program Files
) on Microsoft Windows machines- is it somehow conceptually related to this?
I've seem a description of it from the go team, but it's too practical, it talks about what it is, as opposed to why it is.
So then, why is it?
答案1
得分: 3
这是一个“包含路径”。几乎每种(现代)语言都使用它。
对于C/C++来说,在Unix/Makefile环境中,它是LIB
和INC
环境变量的组合。
对于Perl(5)来说,它是PERLLIB
或PERL5LIB
环境变量。
对于Python来说,它是PYTHONHOME
环境变量。
对于Node.js来说,它是NODE_PATH
变量。
等等,等等。
英文:
It's an "include path". Practically every (modern) language uses one.
For C/C++, it's the combination of the LIB
and INC
environment variables (at least in Unix/Makefile environments).
For Perl (5) it's the PERLLIB
or PERL5LIB
environment variables.
For Python it's the PYTHONHOME
environment variable.
For Node.js it's the NODE_PATH
variable.
Etc, etc.
答案2
得分: 1
GOPATH
是一个变量,用于指示你的应用程序的依赖项安装的位置。它基本上是一个路径,指向存储你的应用程序可能使用的包的目录。
任何一个规模合理的应用程序都有依赖项。在Go语言中,这些依赖项以包的形式存在。在编译时,需要知道依赖项(即你使用的包)的位置,以便能够构建可执行文件。
它们可以存储在固定的预定义位置,也可以让用户自己指定位置。第一种方法有很多缺点(例如,无法支持具有不同目录结构的操作系统)。因此,Go工具的设计者决定通过这个变量来使其可配置。这也给用户带来了更多的灵活性,例如能够将不同项目的依赖项分组存储在不同的目录中。
使用环境变量(如GOPATH
)的方式不仅限于Go语言。Java有它的CLASSPATH
,Python有它的PYTHONPATH
等等(每个语言都有其特点,但基本角色相同)。
英文:
GOPATH
is a variable that indicates where the dependencies of your application are installed. It is basically a path to a directory where you store the packages your application might use.
Any application of a reasonable size has dependencies. In golang, these come in the form of packages. At compile time, the location of the dependencies (i.e. packages you use) needs to be known such that your executable can be built.
They can either be stored at a fixed, predefined location or make somehow the user be able to specify the location himself. The first approach has a lot of downsides (for example, it would be impossible to support operating systems with different directory structure). Thus, the designers of the go tool decided to make it user configurable by the means of this variable. This also gives the user much more flexibility, such as the ability to group the dependencies for different projects in different directories.
The usage of a environment variable (as GOPATH
) is not limited to golang. Java has its CLASSPATH
, Python its PYTHONPATH
and so on (each of them with their quirks, but with the same basic role).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论