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

陌路散爱 提交于 2020-05-29 05:52:13

问题


I just want to use a local package using go modules.

I have these files in a folder goweb:

and go.mod

module goweb

go 1.12

require mypack v0.0.0

replace mypack => ./src/mypack

But go.mod complains:

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

go get -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


回答1:


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.

If they are not part of the same go module, then you can follow these steps:

The path you specify for the replace directive must be either an absolute path or a relative path, relative to the module's root.

So if mypack is a sibling of your module's root, you could use this:

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). Run go mod init mypack in its folder.

Also check out related question: How to use a module that is outside of "GOPATH" in another module?



来源:https://stackoverflow.com/questions/55533971/go-modules-replace-does-not-work-replacement-module-without-version-must-be

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!