![](/img/trans.png)
[英]Go error: go : go.mod file not found in current directory or any parent directory; (working on GOPATH/src)
[英]no required module provides package <imported-package>: go.mod file not found in current directory or any parent directory;
我已经看到可能重复的问题,但他们似乎建议使用 go 模块。 但我想知道为什么记录的功能不能开箱即用:-
导入路径 P 表示 package 位于目录 DIR/src/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 具有 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 {
文件 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 设置正确:-
~/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 'go help modules'
当然,我可以使用 go 模块,但我只是想确保它们不再工作或者我没有遗漏任何东西。
此链接发布在现已删除的评论中 - https://go.dev/ref/mod#mod-commands - 为这个看似 (GO)PATH 的重大变化提供了解释:-
大多数 go 命令可以在模块感知模式或 GOPATH 模式下运行。 在模块感知模式下,go 命令使用 go.mod 文件来查找版本化依赖项,它通常从模块缓存中加载包,如果缺少模块则下载模块。 在 GOPATH 模式下,go 命令忽略模块; 它在供应商目录和 GOPATH 中查找依赖项。
从 Go 1.16 开始,默认启用模块感知模式,无论是否存在 go.mod 文件。 在较低版本中,当当前目录或任何父目录中存在 go.mod 文件时,启用模块感知模式。
更远:-
模块感知模式可以通过 GO111MODULE 环境变量进行控制,该变量可以设置为 on、off 或 auto。
If GO111MODULE=off, the go command ignores go.mod files and runs in GOPATH mode.
通过关闭 GO111MODULE,我可以使用 GOPATH 作为记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.