简体   繁体   English

go 模块 - 替换不起作用 - 没有版本的替换模块必须是目录路径(根或以

[英]go modules - replace does not work - replacement module without version must be directory path (rooted or starting with

I just want to use a local package using go modules.我只想使用 go 模块使用本地 package 。

I have these files in a folder goweb:我在 goweb 文件夹中有这些文件:

在此处输入图像描述

and go.mod和 go.mod

module goweb模块 goweb

go 1.12

require mypack v0.0.0

replace mypack => ./src/mypack

But go.mod complains:但是go.mod抱怨:

replacement module without version must be directory path (rooted or starting with .

go get -u./... go 得到 -u./...

go: parsing src/mypack/go.mod: open <local path>/goweb/src/mypack/go.mod: no such file or directory
go: error loading module requirements

So I am missing some path structure here所以我在这里缺少一些路径结构

If your app and the package it uses are part of the same go module, you don't have to add it to go.mod , you can just refer to it.如果你的应用和它使用的包是同一个 go 模块的一部分,你不必将它添加到go.mod ,你可以直接引用它。

If they are not part of the same go module, then you can follow these steps:如果它们不是同一个 go 模块的一部分,那么您可以按照以下步骤操作:

The path you specify for the replace directive must be either an absolute path or a relative path, relative to the module's root .您为replace指令指定的路径必须是绝对路径或相对路径,相对于模块的 root

So if mypack is a sibling of your module's root, you could use this:因此,如果mypack是模块根目录的同级,则可以使用以下命令:

replace mypack => ../mypack

Also, for this to work, you also have to "convert" mypack into a go module ( mypack must contain a go.mod file).此外,要使其正常工作,您还必须将mypack转换为 go 模块( mypack必须包含go.mod文件)。 Run go mod init mypack in its folder.在其文件夹中运行go mod init mypack

Also check out related question: How to use a module that is outside of "GOPATH" in another module?另请查看相关问题: 如何在另一个模块中使用“GOPATH”之外的模块?

I had this scenario while updating from Go 1.12 to Go 1.19;从 Go 1.12 更新到 Go 1.19 时,我遇到了这种情况; Quite a lot has changed.改变了很多。

I had the Protobuffer files in a separte folder called interfaces out as shown below.我将 Protobuffer 文件放在名为interfaces out 的单独文件夹中,如下所示。

Inside each microservice_x I was creating a directory called generated to hold the protoc generated artefacts.在每个microservice_x中,我创建了一个名为generated的目录来保存 protoc 生成的工件。

Now I need to do a go mod init in the generated folder;现在我需要在generated的文件夹中做一个go mod init along with the replace keywords in respective go mod files以及相应 go mod 文件中的replace关键字

Illustrative code here https://github.com/alexcpn/go_grpc_2022 for better understanding.此处的说明性代码https://github.com/alexcpn/go_grpc_2022以便更好地理解。 Please check the Make file where the build is happening.请检查进行构建的 Make 文件。

go_grpc_2022$ tree
.
├── interfaces
│   └── microservice_1
│       └── test.proto
├── LICENSE
├── microservice_1
│   ├── generated
│   │   ├── go.mod
│   │   ├── microservice_1
│   │   │   ├── test_grpc.pb.go
│   │       └── test.pb.go
│   ├── go.mod
│   ├── go.sum
│   ├── integration_test
│   │   └── validation_test.go
│   ├── Makefile
│   ├── server
│   │   ├── go.mod
│   │   ├── go.sum
│   │   └── server.go
│   ├── test_client
│   │   └── client.go
│   └── test_server
│       ├── go.mod
│       ├── go.sum
│       └── main.go
└── README.md

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

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