golang使用go Modules相关命令时报错

go版本为go 1.16
在使用命令:go mod init 时,报如下错误

go: cannot determine module path for source directory D:\go\code\SipSvrProxy\src\Common (outside GOPATH, module path must be specified)

在这里插入图片描述
之前网上查询了好多方式,都不管用了,最终在 https://golang.google.cn/project 找到相关文档
在这里插入图片描述
进来后,我们找到Tools->Go command->Modules下有如下描述:
在这里插入图片描述

Module-aware mode is enabled by default, regardless of whether a go.mod file is present in the current working directory or a parent directory. More precisely, the GO111MODULE environment variable now defaults to on. To switch to the previous behavior, set GO111MODULE to auto.

Build commands like go build and go test no longer modify go.mod and go.sum by default. Instead, they report an error if a module requirement or checksum needs to be added or updated (as if the -mod=readonly flag were used). Module requirements and sums may be adjusted with go mod tidy or go get.

go install now accepts arguments with version suffixes (for example, go install example.com/cmd@v1.0.0). This causes go install to build and install packages in module-aware mode, ignoring the go.mod file in the current directory or any parent directory, if there is one. This is useful for installing executables without affecting the dependencies of the main module.

go install, with or without a version suffix (as described above), is now the recommended way to build and install packages in module mode. go get should be used with the -d flag to adjust the current module’s dependencies without building packages, and use of go get to build and install packages is deprecated. In a future release, the -d flag will always be enabled.

retract directives may now be used in a go.mod file to indicate that certain published versions of the module should not be used by other modules. A module author may retract a version after a severe problem is discovered or if the version was published unintentionally.
The go mod vendor and go mod tidy subcommands now accept the -e flag, which instructs them to proceed despite errors in resolving missing packages.

The go command now ignores requirements on module versions excluded by exclude directives in the main module. Previously, the go command used the next version higher than an excluded version, but that version could change over time, resulting in non-reproducible builds.

In module mode, the go command now disallows import paths that include non-ASCII characters or path elements with a leading dot character (.). Module paths with these characters were already disallowed (see Module paths and versions), so this change affects only paths within module subdirectories.

google翻译出来就是:

Module-aware模式默认启用,无论 go.mod 文件是否存在于当前工作目录或父目录中。更准确地说,GO111MODULE 环境变量现在默认为 on。要切换到之前的行为,请将 GO111MODULE 设置为 auto。

默认情况下,像 go build 和 go test 这样的构建命令不再修改 go.mod 和 go.sum。相反,如果需要添加或更新模块要求或校验和,它们会报告错误(就像使用了 -mod=readonly 标志一样)。模块要求和总和可以通过 go mod tidy 或 go get 进行调整。

go install 现在接受带有版本后缀的参数(例如,go install example.com/cmd@v1.0.0)。这会导致 go install 以模块感知模式构建和安装包,忽略当前目录或任何父目录中的 go.mod 文件(如果有)。这对于在不影响主模块的依赖关系的情况下安装可执行文件很有用。

go install,带或不带版本后缀(如上所述),现在是在模块模式下构建和安装包的推荐方式。 go get 应该与 -d 标志一起使用来调整当前模块的依赖项,而无需构建包,并且不推荐使用 go get 来构建和安装包。在未来的版本中, -d 标志将始终启用。

现在可以在 go.mod 文件中使用收回指令来指示模块的某些已发布版本不应被其他模块使用。模块作者可能会在发现严重问题或无意发布版本后撤回版本。

go mod vendor 和 go mod tidy 子命令现在接受 -e 标志,该标志指示它们尽管在解析丢失的包时出错,仍继续进行。

go 命令现在忽略对主模块中的 exclude 指令排除的模块版本的要求。以前,go 命令使用比排除版本更高的下一个版本,但该版本可能会随着时间的推移而改变,从而导致不可重现的构建。

在模块模式下,go 命令现在不允许包含非 ASCII 字符或带有前导点字符 (.) 的路径元素的导入路径。带有这些字符的模块路径已被禁止(请参阅模块路径和版本),因此此更改仅影响模块子目录中的路径。

go mod 某些命令,需要带参数,否则要报错。
使用示例:

go mod init Common

输出:

go: creating new go.mod: module Common
go: to add module requirements and sums:
        go mod tidy

在这里插入图片描述
此时再使用其他go mod命令就不会报错了。
如:

go mod tidy
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SunkingYang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值