How to install golang tools globally and then use them in multiple projects in different GOPATH?

huangapple go评论99阅读模式
英文:

How to install golang tools globally and then use them in multiple projects in different GOPATH?

问题

大多数类似golint、gopkgs等的golang工具都是通过go get命令安装的,例如go get -v github.com/golang/lint/golintgo get -v github.com/tpng/gopkgs。我想知道为什么这些工具不像go fmt那样,直接通过源代码运行而不是作为库安装?

由于我同时在多个Go项目上工作,我更喜欢为不同的项目设置不同的GOPATH,因此我必须将这些工具安装到每个项目中,以便进行代码检查或自动补全。

我是不是做错了什么,或者有没有一种方法可以全局安装这些工具,然后在多个项目中使用它们?处理多个项目的人是如何管理这个问题的?

编辑:

我不是在询问库的依赖管理或者同时使用不同版本的Go的问题。我的问题是为什么需要将lint和gopkgs等工具安装到每个GOPATH中,为什么它们被设计成像库一样处理,而不是像go fmt那样提供一个全局的可执行文件,这样就可以在多个项目中使用,就像我们使用go fmt一样。

英文:

Most of the golang tools like golint, gopkgs etc are installed like libraries with go get for instance, go get -v github.com/golang/lint/golint or go get -v github.com/tpng/gopkgs. I wonder why these are not just binaries that run through the source code, like go fmt, for example?

Since I work on multiple Go projects at the same time, I prefer having different GOPATH for different projects and so I am having to install these tools into every single project so that I can lint or have auto completions.

Am I doing something wrong or is there a way to install these tools globally and then just use them in multiple projects? How do people handling multiple projects manage this?

EDIT:

I am not asking about vendoring of libraries or about projects using different versions of Go at the same time. My question is about having to install tools like lint and gopkgs into every GOPATH, why they were designed to be treated like libraries instead of being provided as a global binary like go fmt which then could've been used in multiple projects, just like we use go fmt

答案1

得分: 6

所以我必须将这些工具安装到每个项目中,以便进行代码检查或自动补全。

不,你不需要这样做。可以通过Visual Studio Code的Microsoft vscode-go插件来实现(自2017年1月0.6.53版本以来)。

新的设置go.toolsGopath用于提供一个替代位置来安装扩展所依赖的所有Go工具,如果你不想让它们混杂在你的GOPATH中。请参见PR 351和PR 737。

安装在一个共同的文件夹中的工具有:

'gocode': 'github.com/nsf/gocode',
'gopkgs': 'github.com/tpng/gopkgs',
'go-outline': 'github.com/ramya-rao-a/go-outline',
'go-symbols': 'github.com/acroca/go-symbols',
'guru': 'golang.org/x/tools/cmd/guru',
'gorename': 'golang.org/x/tools/cmd/gorename',
'gomodifytags': 'github.com/fatih/gomodifytags',
'impl': 'github.com/josharian/impl'

(还有一些其他工具,如godocgoimportsgoreturnsdlv等)。

这意味着你的GOPATH由以下组成:

  • 你特定项目的工作区文件夹
  • 一个专门用于所有项目使用的工具的全局go.toolsGopath工作区文件夹。
    这些工具被安装/更新在后者工作区的bin/子文件夹中。

你也可以手动完成这个过程(不使用Visual Studio Code):只需将GOPATH设置为全局工具文件夹,以便在需要安装/更新工具时使用。

然后将GOPATH重置为my/project/dedicated/workspace;/tools/workspace,并将这两个bin/子文件夹添加到你的$PATH/%PATH%中。

OP Nithin在评论中补充道:

这些工具可以编译为二进制文件,如果这些二进制文件在$PATH中可用,大多数编辑器(根据你的帖子)如vscode和atom(go-plus)将能够正常工作,而不会再次执行go get

如果将它们视为库来处理,更新会更容易。(我的意思是go get)。

英文:

> so I am having to install these tools into every single project so that I can lint or have auto completions.

No you don't: see how Visual Studio Code does it through its Microsoft vscode-go plugin (since its 0.6.53 version, January 2017).

> New setting go.toolsGopath, for providing an alternate location to install all the Go tools that the extension depends on, if you don't want them cluttering your GOPATH.
See PR 351 and PR 737

The tools installed in that one common `` folder are:

'gocode': 'github.com/nsf/gocode',
'gopkgs': 'github.com/tpng/gopkgs',
'go-outline': 'github.com/ramya-rao-a/go-outline',
'go-symbols': 'github.com/acroca/go-symbols',
'guru': 'golang.org/x/tools/cmd/guru',
'gorename': 'golang.org/x/tools/cmd/gorename',
'gomodifytags': 'github.com/fatih/gomodifytags',
'impl': 'github.com/josharian/impl'

(and a few others, around godoc, goimports or goreturns, dlv, ...),

That means your GOPATH is composed of:

  • your project-specific workspace folder
  • a global go.toolsGopath workspace folder dedicated to tools used by all projects.
    The tools are installed/updated in the bin/ subfolder of that latter workspace.

You can do that manually too (without Visual Studio Code): simply set GOPATH to that global tools folder whenever you want to install/update the tools.

Then reset GOPATH to my/project/dedicated/workspace;/tools/workspace, and add both bin/ subfolders to your $PATH/%PATH%.

The OP Nithin adds in the comments:

> these tools can be compiled to binaries and if those binaries are available in $PATH, most editors, as far as I tested (based on your post), both vscode and atom (go-plus) will work and wont go get them again.
>
> It is easier to update if they are treated like libraries. (I mean go get)

huangapple
  • 本文由 发表于 2017年7月2日 00:44:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/44863276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定