简体   繁体   English

go 模块安装 go 工具

[英]go modules installing go tools

I'm using go modules as dependency management, and I'm having problem to install something like this:我使用 go 模块作为依赖项管理,我在安装这样的东西时遇到了问题:

go get -u github.com/go-critic/go-critic/...

the result from above was:上面的结果是:

go: cannot find main module; see 'go help modules'

Several of the other answers here have grown stale at this point.在这一点上,这里的其他几个答案已经过时了。

There are at least two cases to consider:至少有两种情况需要考虑:

Case 1情况1

You want to install a tool, but you don't want to modify your current go.mod to track that tool as a dependency.您想安装一个工具,但不想修改当前的go.mod以将该工具作为依赖go.mod进行跟踪。

In short, with Go 1.12 or 1.13, the simplest solution is to cd to a directory without a go.mod , such as:简而言之,对于 Go 1.12 或 1.13,最简单的解决方案是cd到一个没有go.mod的目录,例如:

$ cd /tmp
$ go get github.com/some/tool@v1.0.1

Alternatively, gobin is a module-aware command to install or run binaries that provides additional flexibility, including the ability to install without altering your current module's go.mod或者, gobin是一个模块感知命​​令,用于安装或运行二进制文件,提供额外的灵活性,包括在不改变当前模块的go.mod情况下进行安装的能力

See this related answer for more details, including a solution for Go 1.11, as well as the likely new option in Go 1.14 for getting a tool without it updating your go.mod .有关更多详细信息,请参阅此相关答案,包括 Go 1.11 的解决方案,以及 Go 1.14 中可能的新选项,用于在不更新go.mod情况下获取工具。

Case 2案例二

On the other hand, if you want to explicitly track a tool as a versioned dependency in your go.mod , then see the "How can I track tool dependencies for a module?"另一方面,如果您想在go.mod工具显式跟踪为版本化依赖go.mod ,请参阅“如何跟踪模块的工具依赖项?” FAQ on the modules wiki.模块 wiki 上的常见问题解答。

In short, you create a tools.go file in a separate package, and set a // +build tools build tag, such as:简而言之,你在单独的包中创建一个tools.go文件,并设置一个// +build tools构建标签,例如:

// +build tools

package tools

import (
    _ "golang.org/x/tools/cmd/stringer"
)

The import statements allow the go command to precisely record the version information for your tools in your module's go.mod , while the // +build tools build constraint prevents your normal builds from actually importing your tools. import 语句允许go命令在模块的go.mod精确记录工具的版本信息,而// +build tools构建约束阻止您的正常构建实际导入您的工具。

Edit: The original answer herein referred specifically to the state of the tooling in Go 1.11.编辑:此处的原始答案特指 Go 1.11 中工具的状态。 Since the release of Go 1.12, this is no longer accurate.自 Go 1.12 发布以来,这不再准确。 Please see this answer , and the ones it links to, for details of handling this situation in Go 1.12 and later.有关在 Go 1.12 及更高版本中处理这种情况的详细信息,请参阅此答案及其链接的答案

If the GO111MODULE var is set to on , you have to be inside an initialized go module directory tree in order to use go get , even if you're trying to get a tool rather than a new dependency .如果GO111MODULE var 设置为on ,您必须在初始化的 go 模块目录树中才能使用go get ,即使您尝试获取工具而不是新的依赖项 This is a known and heavily debated issue:这是一个众所周知且备受争议的问题:

https://github.com/golang/go/issues/27643 https://github.com/golang/go/issues/27643

https://github.com/golang/go/issues/24250 https://github.com/golang/go/issues/24250

https://github.com/golang/go/issues/25922 https://github.com/golang/go/issues/25922

The solution, short term, is to run GO111MODULE=off go get <tool> .短期的解决方案是运行GO111MODULE=off go get <tool> This explicitly disables the module support, even if you're in a module package currently, and forces it to only utilize your GOPATH.这会明确禁用模块支持,即使您当前位于模块包中,并强制它仅使用您的 GOPATH。

Long-term, figuring out what the best solution is to support tool install via go get (or another command, like go install with a flag) is an ongoing area of discussion with little in the way of established consensus as of yet.从长远来看,找出通过go get (或其他命令,如带有标志的go install )支持工具安装的最佳解决方案是一个持续的讨论领域,目前几乎没有达成共识。 However, there's a PR open for Go 1.12 that, if accepted, will allow go get to simply work while outside a module, even with GO111MODULE=on set.然而,Go 1.12 有一个PR 开放,如果被接受,它将允许go get在模块之外简单地工作,即使GO111MODULE=on设置。

With Go 1.12 (February 2019), GO111MODULE=on go get will work.Go 1.12 (2019 年 2 月)中, GO111MODULE=on go get将起作用。
(From issue 24250 ) (来自第 24250 期

Modules模块

When GO111MODULE is set to on , the go command now supports module-aware operations outside of a module directory, provided that those operations do not need to resolve import paths relative to the current directory or explicitly edit the go.mod file.GO111MODULE设置为ongo命令现在支持模块目录之外的模块感知操作,前提是这些操作不需要解析相对于当前目录的导入路径或显式编辑go.mod文件。
Commands such as go get , go list , and go mod download behave as if in a module with initially-empty requirements.诸如go getgo listgo mod download行为就像在具有初始为空要求的模块中一样。
In this mode, go env GOMOD reports the system's null device ( /dev/null or NUL ).在这种模式下, go env GOMOD报告系统的空设备( /dev/nullNUL )。

I faced the same issue and resolved it by the below command.我遇到了同样的问题并通过以下命令解决了它。

$ go env -w GO111MODULE=auto $ go env -w GO111MODULE=auto

试试这个命令GO111MODULE=on go get -u github.com/go-critic/go-critic/...

Just had the same issue on go1.11.2.刚刚在 go1.11.2 上遇到了同样的问题。 Tried to set GO111MODULE=on , but this couldn't fix it.试图设置GO111MODULE=on ,但这无法修复它。

My solution:我的解决方案:

  1. Upgrade to use go 1.12.5: https://golang.org/doc/install?download=go1.12.5.linux-amd64.tar.gz升级使用 go 1.12.5: https ://golang.org/doc/install ? download = go1.12.5.linux-amd64.tar.gz

    This can resolve go: cannot find main module; see 'go help modules'这可以解决go: cannot find main module; see 'go help modules' go: cannot find main module; see 'go help modules' issue. go: cannot find main module; see 'go help modules'问题。

  2. Then I got another issue go: cannot use path@version syntax in GOPATH mode ,然后我遇到了另一个问题go: cannot use path@version syntax in GOPATH mode

    which can be resolved setting env GO111MODULE=on这可以通过设置 env GO111MODULE=on解决

I had the same problem and it was "almost" solved directly.我遇到了同样的问题,“几乎”直接解决了。 At first it did not work.起初它不起作用。 This was because the module was already initialized and I had run the "go get..." command before I had set GO111MODULE=on.这是因为模块已经初始化并且我在设置 GO111MODULE=on 之前运行了“go get...”命令。

To get it working I removed "go.mod" reinitialized the mod by running "go mod init ..." and re run the necessary "go get ...." commands.为了让它工作,我删除了“go.mod”,通过运行“go mod init ...”重新初始化了mod,并重新运行必要的“go get ....”命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM