简体   繁体   English

Go 模块替换命令不起作用

[英]Go modules replace command is not working

I have following code structure for go module example (Go 1.14.2 on macOS)我有以下 go 模块示例的代码结构(macOS 上的 Go 1.14.2)

BookingNexus BookingNexus

  • go.mod go.mod
  • main.go主.go
  • server.go服务器.go

I added following dependency for server.go我为server.go添加了以下依赖项

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

I added following code to go.mod我将以下代码添加到 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

Since I don't have nurture.gitlab.com right now, so I used replace directive, howerver it's still trying to download from nurture.gitlab.com and saying that unable to dail tcp. Since I don't have nurture.gitlab.com right now, so I used replace directive, howerver it's still trying to download from nurture.gitlab.com and saying that unable to dail tcp. Generating following error if I run go mod tidy如果我运行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

I expect replace command should provide alternative way to get the path, however it's still trying to download from unexistenet path and not considering replace directive at all (I tried including and excluding, got same message both times)我希望替换命令应该提供获取路径的替代方法,但是它仍在尝试从不存在的路径下载并且根本不考虑替换指令(我尝试包含和排除,两次都收到相同的消息)

Can somebody help me why is this is the case?有人可以帮我为什么会这样吗?

If you want to build against your local copy of the package Contracts , you need to comment out the remote path:如果要针对 package Contracts的本地副本进行构建,则需要注释掉远程路径:

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

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

Once the development is complete and checked into the remote location, then comment out the replace and restore the remote-path.开发完成并签入远程位置后,注释掉replace并恢复远程路径。

Some things of note from the golang wiki which apply to your case: 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.注意:如果替换指令的右侧是文件系统路径,则目标必须在该位置具有 go.mod 文件。 If the go.mod file is not present, you can create one with go mod init.如果 go.mod 文件不存在,您可以使用 go mod init 创建一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM