Go 模块替换命令不起作用

标签 go go-modules

我有以下 go 模块示例的代码结构(macOS 上的 Go 1.14.2)

预订Nexus

  • go.mod
  • main.go
  • server.go

我为server.go添加了以下依赖项

package main
import bn "nurture.gitlab.com/Core/Contracs/BookingNexus/Gen/Go"

我在 go.mod 中添加了以下代码

module nurture.gitlab.com/Core/BookingNexus

go 1.14

require (
    nurture.gitlab.com/Core/Contracts latest
)

replace nurture.gitlab.com/Core/Contracts latest => /Users/.../Core/Contracts

由于我现在没有 Nuret.gitlab.com,所以我使用了 replace 指令,但是它仍然尝试从 Nuret.gitlab.com 下载并说无法 dail tcp。 如果我运行 go mod tidy

则会生成以下错误
/Users/.../BookingNexus/go.mod:6: unrecognized import path "nurture.gitlab.com/Core/Contracts": https fetch: Get "https://nurture.gitlab.com/Core/Contracts?go-get=1": dial tcp: lookup nurture.gitlab.com: no such host

我期望替换命令应该提供获取路径的替代方法,但是它仍然尝试从不存在的路径下载并且根本不考虑替换指令(我尝试包含和排除,两次都收到相同的消息)

有人可以帮我看看为什么会出现这种情况吗?

最佳答案

如果您想根据包 Contracts 的本地副本进行构建,则需要注释掉远程路径:

require (
    //
    // comment this out during local development:
    //
    // nurture.gitlab.com/Core/Contracts latest
)

replace nurture.gitlab.com/Core/Contracts => /Users/.../Core/Contracts

开发完成并 checkin 远程位置后,注释掉替换并恢复远程路径。

golang wiki 中的一些值得注意的事情适用于您的情况:

Note: if the right-hand side of a replace directive is a filesystem path, then the target must have a go.mod file at that location. If the go.mod file is not present, you can create one with go mod init.

关于Go 模块替换命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61736850/

相关文章:

go - 获取 : go: error loading module requirements

Go模块: main. go文件在go构建后被覆盖

go - 弃用和收回之间的区别?

go - 构建命令行参数 : cannot load local package: cannot find module providing package

go - cgo调用共享库: cannot find lib or function?

go - 将 HTTP 响应写入临时 bytes.Buffer

go - 使用 Go 依赖项的修改版本的问题

go - AM2320 传感器 : CRCs doesn't match, 来自传感器的 CRC(0)

performance - 在 golang 中,为什么当我使用缓冲(异步) channel 时我的程序运行速度变慢?

go - JWT不验证收到的 token