英文:
Can someone explain why GOPATH is convenient and how it should be used in general?
问题
我是新接触Go编程语言的,每个教程都从设置GOPATH到当前项目文件夹开始。
我有什么遗漏吗?程序员真的应该在进入新的Go项目文件夹时手动设置GOPATH吗?我已经阅读了几个关于GOPATH的常见问题解答,但仍然无法理解。
那么为什么还有GOROOT呢?它的目的是什么?
是否有任何自动工具可以检测当前目录是否是Go项目的根目录(例如通过某个隐藏文件),并自动将GOPATH更改为该目录?
谢谢,非常感谢任何建议。
附:例如,我开发完全独立的Go项目A、B和C,它们应该存在于单个的“工作区”环境中吗?我猜不是,但那我应该如何处理GOPATH和GOROOT呢?
英文:
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?
答案1
得分: 42
GOPATH
的目标是将所有的包集中到一个共同的工作空间中。它本身并不是一个新概念(可以想象一下Java的类路径),但Go的使用方式更简单,因为它不支持包的版本控制。
当进入一个新的项目文件夹时,Go程序员不需要手动设置GOPATH
。每个项目文件夹本身就应该是一个包,并且与其他包一起存放在GOPATH
中,所以GOPATH
只需要设置一次。教程通常会在开始时设置GOPATH
,以便将教程的工作空间与其他内容隔离开来(或者假设用户尚未设置GOPATH
)。
GOROOT
用于为Go程序员提供标准包,你不需要对其进行任何操作。简而言之,对于GOROOT
有一个基本规则:绝对不要触碰它。不要在其中安装任何东西,不要修改标准包等。
我不知道有没有工具可以检测当前目录中的Go项目,但创建一个这样的工具应该不会非常复杂。
如何处理不同的项目完全取决于你。Go的方式是将每个项目作为一个包放在$GOPATH/src
目录中,并从那里进行所有操作。但我个人不太喜欢这种方式,我将我的GOPATH
定义为$HOME/.go
。然后我将每个项目放在计算机的其他地方的一个专用目录中,并将项目目录的符号链接放入$GOPATH/src
目录中。然后我就可以使用每个Go工具链命令(例如go build myproject
),将该项目作为另一个项目的包使用,等等。
英文:
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's use is drastically simpler by not supporting packages versioning.
The Go programmer isn't supposed 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 else (or simply assuming that a user hasn't set the GOPATH
, yet).
GOROOT
is set to provide the standard packages to the Go programmer, you don't need to do anything with it. In 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 different projects is up to you. The Go way is to put every project as a package in the $GOPATH/src
directory 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 Go toolchain command (e.g. go build myproject
), use the project as package for another one, etc.
答案2
得分: 12
GOPATH允许你将依赖的源代码和编译后的二进制文件收集到一个地方。这似乎是一个非常吸引人的想法。然而,我发现自己在几个完全无关的Go项目上工作,并且另一种方法更适合我。
这是一种类似但不同于Elwinar的symlnks的策略。我在一个空文件夹中开始一个新项目,并创建src
文件夹。然后我在这个文件夹中放入一个名为env.sh的shell脚本:
if [ `type -p go` = "" ]; then
export PATH=$PATH:/usr/local/go/bin
fi
export GOPATH=$PWD
export PATH=$PATH:$PWD/bin
每次我开始工作时,我使用以下命令:
. env.sh
注意点和空格-它们很重要。
现在,我在这个项目中所做的一切都局限在这个文件夹内。这可能不是最常用的策略,但对我来说效果很好。
还有一件事:如果你的依赖项在测试等方面使用环境变量,你也可以将它们放在env.sh
中。例如,Gorp有以下内容:
export GORP_TEST_DSN=test/testuser/TestPasswd9
export GO_TEST_DSN=testuser:TestPasswd9@/test
附加说明
在最新的Go版本中,GOPATH
是可选的;如果你不设置它,它的默认值是$HOME/go。如果你设置了它并且想要使用新的模块功能,还要设置GO111MODULES=on
。
英文:
GOPATH allows you to collect dependency source code and the resulting compiled binaries in one place. This seems like a really attractive idea. However, I found myself working on several totally unrelated Go projects and an alternative approach suited me better.
This is a similar but different strategy to Elwinar's symlnks. I start a new project in an empty folder and create src
. And I drop into the folder this shell script called env.sh:
if [ `type -p go` = "" ]; then
export PATH=$PATH:/usr/local/go/bin
fi
export GOPATH=$PWD
export PATH=$PATH:$PWD/bin
Each time I start work, I use
. env.sh
Note the dot and space - they matter.
Now, everything I do on this project is localised within this folder. It's possibly not the most widely-used strategy, but it works well for me.
And another thing: if your dependencies make use of environment variables for testing etc, you can put them in env.sh
too. For example, Gorp has
export GORP_TEST_DSN=test/testuser/TestPasswd9
export GO_TEST_DSN=testuser:TestPasswd9@/test
Addendum
In the most recent Go versions, GOPATH
is optional; if you don't set it, the default is $HOME/go. If you do set it and also want to use the new modules feature, set GO111MODULES=on
also.
答案3
得分: 10
你不需要设置GOPATH或GOROOT。默认情况下,GOPATH位于用户/主目录下。
如果未设置GOPATH,则在Unix系统上默认为$HOME/go,在Windows上默认为%USERPROFILE%\go。如果要使用自定义位置作为工作区,可以设置GOPATH环境变量。
Go模块
现在有Go模块支持(自Go 1.11起),因此您不再需要使用GOPATH。例如,您可以转到系统上的任何目录($GOPATH之外),然后在那里初始化一个新的Go模块,然后开始在那里工作。不需要GOPATH。
您只需要在目录中执行一次以下操作:
go mod init
$GOPATH:Go将这些文件存储在其中:
- 源文件($GOPATH/src)
- 编译的包文件($GOPATH/pkg)
- 可运行文件($GOPATH/bin)
$GOROOT:Go源代码所在的位置,例如Go标准库。
此外,为了能够从系统的任何位置运行任何已安装的可执行文件,您可能希望将$GOPATH/bin添加到您的路径环境变量中,如下所示:
export PATH=$PATH:$(go env GOPATH)/bin
更多信息,请查看这里。
英文:
You don't need to set your GOPATH or GOROOT. GOPATH by default is under your user/home directory.
> If no GOPATH is set, it is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. If you want to use a custom location as your workspace, you can set the GOPATH environment variable.
Go Modules
Now there's Go Modules support (since Go 1.11), so you don't have to use GOPATH anymore. For example, you can go to any directory on your system (outside of $GOPATH), and you can initialize a new Go module there, then you start working there. No GOPATH is needed.
You just need to do this once (while in a directory):
go mod init
$GOPATH: Go stores these files under it:
- Source files ($GOPATH/src)
- Compiled package files ($GOPATH/pkg)
- Runnable files ($GOPATH/bin)
$GOROOT: Where the Go source code resides like Go Standard Library.
Also to run any go install
ed executable file from anywhere on your system, you might want to add $GOPATH/bin
to your path environment variable like this:
export PATH=$PATH:$(go env GOPATH)/bin
More information check out this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论