[英]no required module provides package <imported-package>: go.mod file not found in current directory or any parent directory;
I have seen possible duplicate questions but they seem to suggest use of go modules.我已经看到可能重复的问题,但他们似乎建议使用 go 模块。 But I was wondering why a documented functionality 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').
导入路径 P 表示 package 位于目录 DIR/src/P 中,用于 GOPATH 环境变量中列出的某些 DIR(有关更多详细信息,请参阅:'go help gopath')。
I am trying to use root.go from the file main.go我正在尝试使用文件 main.go 中的 root.go
~/src/github.com/himanshugarg/sicp/root$ ls
main.go root.go test.go
The file root.go has the package declaration:-文件 root.go 具有 package 声明:-
~/src/github.com/himanshugarg/sicp/root$ head root.go
// Find square root using Newton's method
// Based on SICP's on page 30
package root
import (
"math"
);
func Sqrt(x float64) float64{
goodEnough := func (guess float64) bool {
And the file main.go imports root:-文件 main.go 导入根目录:-
~/src/github.com/himanshugarg/sicp/root$ head main.go
package main
import (
"fmt"
"os"
"strconv"
"github.com/himanshugarg/sicp/root"
);
func main() {
GOPATH is set correctly:- GOPATH 设置正确:-
~/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 'go help modules'
Surely I can use go modules but I just want to be sure that they no longer work or that I am not missing something.当然,我可以使用 go 模块,但我只是想确保它们不再工作或者我没有遗漏任何东西。
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:-此链接发布在现已删除的评论中 - https://go.dev/ref/mod#mod-commands - 为这个看似 (GO)PATH 的重大变化提供了解释:-
Most go commands may run in Module-aware mode or GOPATH mode.
大多数 go 命令可以在模块感知模式或 GOPATH 模式下运行。 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.
在模块感知模式下,go 命令使用 go.mod 文件来查找版本化依赖项,它通常从模块缓存中加载包,如果缺少模块则下载模块。 In GOPATH mode, the go command ignores modules;
在 GOPATH 模式下,go 命令忽略模块; it looks in vendor directories and in GOPATH to find dependencies.
它在供应商目录和 GOPATH 中查找依赖项。
As of Go 1.16, module-aware mode is enabled by default, regardless of whether a go.mod file is present.
从 Go 1.16 开始,默认启用模块感知模式,无论是否存在 go.mod 文件。 In lower versions, module-aware mode was enabled when a go.mod file was present in the current directory or any parent directory.
在较低版本中,当当前目录或任何父目录中存在 go.mod 文件时,启用模块感知模式。
Further:-更远:-
Module-aware mode may be controlled with the GO111MODULE environment variable, which can be set to on, off, or auto.
模块感知模式可以通过 GO111MODULE 环境变量进行控制,该变量可以设置为 on、off 或 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.通过关闭 GO111MODULE,我可以使用 GOPATH 作为记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.