no required module provides package <imported-package>: go.mod file not found in current directory or any parent directory;

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

no required module provides package <imported-package>: go.mod file not found in current directory or any parent directory;

问题

我看到了可能重复的问题,但它们似乎建议使用go模块。但我想知道为什么GOPATH的文档中所述的功能不能直接使用:

> 导入路径P表示在GOPATH环境变量中列出的某个DIR目录下找到的包(更多详细信息请参阅:'go help gopath')。

我正在尝试从main.go文件中使用root.go

~/src/github.com/himanshugarg/sicp/root$ ls
main.go  root.go  test.go

root.go文件有包声明:

~/src/github.com/himanshugarg/sicp/root$ head root.go 
// 使用牛顿法求平方根
// 基于SICP第30页
package root

import (
  &quot;math&quot;
);

func Sqrt(x float64) float64{
  goodEnough := func (guess float64) bool {

main.go文件导入了root:

~/src/github.com/himanshugarg/sicp/root$ head main.go 
package main

import (
  &quot;fmt&quot;
  &quot;os&quot;
  &quot;strconv&quot;
  &quot;github.com/himanshugarg/sicp/root&quot;
);

func main() {

GOPATH已正确设置:

~/src/github.com/himanshugarg/sicp/root$ echo $GOPATH
/home/hgarg

但是构建失败了:

~/src/github.com/himanshugarg/sicp/root$ go build main.go 
main.go:7:3: no required module provides package github.com/himanshugarg/sicp/root: go.mod file not found in current directory or any parent directory; see &#39;go help modules&#39;

我当然可以使用go模块,但我只是想确保GOPATH不再起作用,或者我是否遗漏了什么。

英文:

I have seen possible duplicate questions but they seem to suggest use of go modules. But I was wondering why the well documented functionality of GOPATH doesn't work out of the box:-

> the import path P denotes the package found in the directory DIR/src/P
> for some DIR listed in the GOPATH environment variable (For more
> details see: 'go help gopath').

I am trying to use root.go from the file main.go

~/src/github.com/himanshugarg/sicp/root$ ls
main.go  root.go  test.go

The file root.go has the package declaration:-

~/src/github.com/himanshugarg/sicp/root$ head root.go 
// Find square root using Newton&#39;s method
// Based on SICP&#39;s on page 30
package root

import (
  &quot;math&quot;
);

func Sqrt(x float64) float64{
  goodEnough := func (guess float64) bool {

And the file main.go imports root:-

~/src/github.com/himanshugarg/sicp/root$ head main.go 
package main

import (
  &quot;fmt&quot;
  &quot;os&quot;
  &quot;strconv&quot;
  &quot;github.com/himanshugarg/sicp/root&quot;
);

func main() {

GOPATH is set correctly:-

~/src/github.com/himanshugarg/sicp/root$ echo $GOPATH
/home/hgarg

But the build fails:-

~/src/github.com/himanshugarg/sicp/root$ go build main.go 
main.go:7:3: no required module provides package github.com/himanshugarg/sicp/root: go.mod file not found in current directory or any parent directory; see &#39;go help modules&#39;

Surely I can use go modules but I just want to be sure that GOPATH no longer works or that I am not missing something.

答案1

得分: 2

这是一个已删除评论中发布的链接:https://go.dev/ref/mod#mod-commands,它解释了这个看似破坏(GO)PATH的变化:

大多数go命令可以在模块感知模式(Module-aware mode)或GOPATH模式下运行。在模块感知模式下,go命令使用go.mod文件来查找版本化的依赖项,并且通常从模块缓存中加载包,如果缺少模块,则会下载。在GOPATH模式下,go命令忽略模块;它在vendor目录和GOPATH中查找依赖项。

从Go 1.16开始,默认情况下启用模块感知模式,无论当前目录或任何父目录中是否存在go.mod文件。在较低版本中,只有在当前目录或任何父目录中存在go.mod文件时,才启用模块感知模式。

此外:

可以使用GO111MODULE环境变量来控制模块感知模式,它可以设置为on、off或auto。

如果GO111MODULE=off,go命令将忽略go.mod文件并以GOPATH模式运行。

通过关闭GO111MODULE,我可以按照文档使用GOPATH。

英文:

This link posted in a now deleted comment - https://go.dev/ref/mod#mod-commands - provides an explanation for this seemingly (GO)PATH breaking change:-

> Most go commands may run in Module-aware mode or GOPATH mode. In
> module-aware mode, the go command uses go.mod files to find versioned
> dependencies, and it typically loads packages out of the module cache,
> downloading modules if they are missing. In GOPATH mode, the go
> command ignores modules; it looks in vendor directories and in GOPATH
> to find dependencies.
>
> As of Go 1.16, module-aware mode is enabled by default, regardless of
> whether a go.mod file is present. In lower versions, module-aware mode
> was enabled when a go.mod file was present in the current directory or
> any parent directory.

Further:-

> Module-aware mode may be controlled with the GO111MODULE environment
> variable, which can be set to on, off, or auto.
>
> If GO111MODULE=off, the go command ignores go.mod files and runs in GOPATH mode.

By turning GO111MODULE off I am able to use GOPATH as documented.

huangapple
  • 本文由 发表于 2021年12月13日 11:19:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/70329612.html
匿名

发表评论

匿名网友

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

确定