问题描述
我是一名新的golang开发人员,我想知道为什么需要在项目的根目录下设置$GOPATH
环境变量.
如果我同时从事多个项目,则每次都需要重新设置$GOPATH
环境变量以指向不同的位置.
在我的设置中,我将$GOPATH
设置为/Users/Projects/go/lib
.这是我所有golang项目的通用目录.
请澄清一下:项目数据放置在
中如果(据我所知)所有$GOPATH
都用于安装第三方库,那么为我所有的项目都拥有一个$GOPATH
目录是不安全的,因此所有必需的第三方库都安装在同一个lib目录中,并且每当我在这些项目上进行编译时,它都将使用所需的库.
这真的不好吗?为什么?
(2018年第二季度:
请注意,使用 vgo项目 ,GOPATH
可能最终因支持基于项目的工作流程.这样可以避免两年前我在下面提出的基于项目的手动GOPATH
)
在Go 1.11(2018年8月)中, GOPATH
可以是可选的,其中模块 .
VSCode越来越支持它:
2016年6月:您不必仅依靠 一个GOPATH
(即一个工作区).
我的完整GOPATH
包括:
- 全局路径(对于所有实用程序,例如
goimports
),github.com/smartystreets/goconvey
,.. ),例如在$HOME/go
中, - 本地路径(对于我当前的项目),我的本地
src
,pkg
和bin
将在其中.
这是两条路径:
export GOPATH=/path/to/myproject:$HOME/go
我不喜欢这种做法,因为不同的项目可能需要同一个库的不同的版本.
这就是为什么每个项目只有一个GOPATH
,我的构建脚本(随项目版本化)为我设置的原因.
当我克隆我的go项目时,我:
- 将我的
GOPATH
设置为该go项目(本地路径,该项目需要的第三方库将安装到该本地路径,并移动到vendor
文件夹中), - 对该路径
<myproject>/src/<myproject> -> ../..
进行符号链接,因为GOPATH
表示go希望在src/<apackage>
中找到myproject
的来源.
该组织:
- 与
go get
保持兼容, - 确保默认情况下,我需要的所有特定依赖项都安装在我的项目文件夹中,而不会因全局
GOPATH
中存在的大量全局库/实用程序而丢失.
我有:
myproject
mysource.go
apackage
othersource.go
src
myproject -> ../..
vendor
third-party packages
在Windows上,典型的构建脚本为:
λ more b.bat
@echo off
setlocal EnableDelayedExpansion
if not defined GOROOT (
echo Environment variable GOROOT must be defined, with %%GOROOT%%\bin\go.exe
exit /b 1
)
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
set PATH=%PATH%;%GOROOT%/bin
set GOPATH=%~dp0;%HOME%/go
set prjname=%GOPATH:~0,-1%
for %%i in ("%prjname%") do set "prjname=%%~ni"
rem echo prjname='%prjname%'
if not exist src (
mkdir src
)
if not exist src\%prjname% (
mklink /J src\%prjname% %GOPATH%
)
pushd %~dp0
cd src\%prjname%
rem cd
go install
popd
endlocal
克隆我的go项目的任何人都只需键入"b
".
I'm a new golang developer and I wonder why $GOPATH
environment variable is needed to be set at the root of my project.
If I'm working on several projects at the same time, I need to each time re-set the $GOPATH
environment variable to point to a different location.
In my setup, I have $GOPATH
set to /Users/Projects/go/lib
. which is a generic directory for all of my golang projects.
Just to clarify: the projects data is placed in /Users/Projects/go/<Project Name>
If anyhow all $GOPATH
is used for (as far as I know) is to install 3rd party libraries, isn't it safe to have one $GOPATH
directory for all my projects, so all the required 3rd party libraries are installed in the same lib directory, and whenever I compile on of the projects it just uses the libs it requires.
Is this bad in practice? Why?
(Q2 2018:
Note that with the vgo project, GOPATH
might end up being deprecated in favor of a project-based workflow. That would avoid the manual project-based GOPATH
I was proposing below, two years ago)
With Go 1.11 (August 2018), GOPATH
can be optional, with modules.
It is more and more supported with VSCode:
June 2016: You don't have to rely on only one GOPATH
(ie one workspace).
My full GOPATH
includes:
- a global path (for all utilities like
goimports
),github.com/smartystreets/goconvey
, ...), in$HOME/go
for instance, - a local path (for my current project), where my local
src
,pkg
andbin
will be.
That is two paths:
export GOPATH=/path/to/myproject:$HOME/go
I don't like that practice, as different projects could require different version of the same library.
That is why I have one GOPATH
per project, that my build script (versioned with the project) sets for me.
When I clone a go project of mine, I:
- set my
GOPATH
to that go project (local path, where the third-party libraries I need for that project will be installed, and moved to avendor
folder), - make a symlink to that path
<myproject>/src/<myproject> -> ../..
, sinceGOPATH
means go expects to find the sources ofmyproject
insrc/<apackage>
.
That organization:
- remains compatible with
go get
, - ensure any specific dependencies I need are installed by default in my project folder instead of being lost within the mass of global libraries/utilities present in the global
GOPATH
.
I have:
myproject
mysource.go
apackage
othersource.go
src
myproject -> ../..
vendor
third-party packages
On Windows, a typical build script would be:
λ more b.bat
@echo off
setlocal EnableDelayedExpansion
if not defined GOROOT (
echo Environment variable GOROOT must be defined, with %%GOROOT%%\bin\go.exe
exit /b 1
)
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
set PATH=%PATH%;%GOROOT%/bin
set GOPATH=%~dp0;%HOME%/go
set prjname=%GOPATH:~0,-1%
for %%i in ("%prjname%") do set "prjname=%%~ni"
rem echo prjname='%prjname%'
if not exist src (
mkdir src
)
if not exist src\%prjname% (
mklink /J src\%prjname% %GOPATH%
)
pushd %~dp0
cd src\%prjname%
rem cd
go install
popd
endlocal
Anyone cloning my go project would simply type 'b
'.
这篇关于有关$ GOPATH的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!