在每个项目的基础上自动定义GOPATH [英] Automatically define GOPATH on a per project basis

查看:183
本文介绍了在每个项目的基础上自动定义GOPATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我创建的每个项目,每次我进入项目目录时都必须执行export GOPATH={path_to_project}.必须有一种更简单的方法.我没有办法为给定目录创建.bashrc或.bash_profile文件来为该项目定义GOPATH吗?

For every project I create, I have to do export GOPATH={path_to_project} every time I cd into the project dir. There has to be an easier way. Isn't there some way I can create a .bashrc or .bash_profile file for a given directory to define the GOPATH for that project?

例如,我有两个go项目A和B.如果我有一个单一的GOPATH,但在两个项目之间移动时未重新定义它,则两个项目的二进制文件都将存储在同一位置.更重要的是,第三方库的二进制文件将存储在同一位置,因此我无法在每个项目的基础上维护同一库的多个版本.

For example, I have two go projects A and B. If I have a singular GOPATH that isn't redefined when I move between projects, then binaries for both projects will be stored in the same place. More importantly, binaries for third party libraries will be stored in the same place, so I have no way of maintaining multiple versions of the same library on a per project basis.

但是,如果我能够在每个项目的基础上定义GOPATH,则所有二进制文件和第三方库都取决于项目.这似乎是大多数其他语言环境(ruby rbenv,python vertiualenv等)中处理软件包管理的常用方法.

However, if I am able to define GOPATH on a per project basis, then all binaries and third party libraries are project dependent. This seems to be the common way of handling package management in most other language environments (ruby rbenv, python vertiualenv, etc.)

推荐答案

(2018年第二季度: 请注意,对于 vgo(现为模块")项目GOPATH可能最终因支持项目而被弃用基于工作流程.这样可以避免两年前我在下面提出的基于项目的手动GOPATH)

(Q2 2018: Note that with the vgo (now "module") 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)

在Go 1.11(2018年8月)中, GOPATH对于模块可以是可选的.

With Go 1.11 (August 2018), GOPATH can be optional, with modules.

您在> 轻松管理多个GOPATH目录中表达了类似的想法 ,由 Herbert Fischer(hgfischer)用于Linux/Unix环境(基于<一个href ="https://stackoverflow.com/a/8691437/6309">该问题已在上面的评论中提及):

You have a similar idea expressed in Manage multiple GOPATH dirs with ease, by Herbert Fischer (hgfischer), for a Linux/Unix environment (base on the question already mention in the comments above):

只需在您的~/.bashrc(或~/.bash_profile)中包含以下代码段,然后使用source ~/.bashrc重新加载您的Shell环境.
此代码段将创建一个shell函数,该函数将使用自定义的命令覆盖内置命令cd,该命令将扫描输入的目录以及上面的其他命令,以查找名为.gopath的文件.

Just include the following snippet in your ~/.bashrc (or ~/.bash_profile) and reload your shell environment with source ~/.bashrc.
This snippet will create a shell function that will override the builtin command cd with a customized one that scans the entered directory, and every other above, for a file named .gopath.

cd () {
    builtin cd "$@"
    cdir=$PWD
    while [ "$cdir" != "/" ]; do
        if [ -e "$cdir/.gopath" ]; then
            export GOPATH=$cdir
            break
        fi
        cdir=$(dirname "$cdir")
    done
}

现在,您只需要在要用作GOPATH的每个目录中创建一个.gopath文件,并且每次进入此目录时,重新定义的cd函数会将当前环境的GOPATH设置为此目录.

Now you just need to create a .gopath file in every directory you want as your GOPATH and every time you enter this directory, the redefined cd function will set the GOPATH of your current environment to this directory.


2017年更新:如果您不想修改环境,则仍然可以通过在Visual Studio Code中打开该项目的src文件夹(


Update 2017: if you don't want to modify your environment, you can still use one GOPATH per project, by opening the src folder of that project in Visual Studio Code (vscode, which is a multi-platform IDE), combined with the extension "Go for Visual Studio Code".

在该IDE中,您可以:

In that IDE, you can:

  • 在名为go.toolsGopath的设置中保留全局唯一GOPATH.
  • 使用名为go.inferGopath
  • 的设置推断您当前的GOPATH
  • keep your global unique GOPATH in a setting called go.toolsGopath.
  • infer your current GOPATH with a setting called go.inferGopath

这样,VSCode将在全局GOPATH中安装一组工具(供您在VSCode之外使用).
请参阅"
Go工具,即Go扩展名取决于:godepgolintgurugodoc,...

That way, VSCode will install a collection of tools in your global GOPATH (for you to use outside VSCode as well).
See "Go tools that the Go extension depends on": godep, golint, guru, godoc, ...

但是,您的项目的GOPATH将是src的父文件夹:

And yet, your GOPATH for your project will be the parent folder of src:

当您从IDE编译/安装项目时,此方法有效.
如果您想从命令行执行此操作,则上面的原始答案仍然适用.

That works when you compile/install your project from the IDE.
If you want to do it from the command line, the original answer above would still apply.

这篇关于在每个项目的基础上自动定义GOPATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆