Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

go-modules

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

I have these files in a folder goweb:

enter image description here

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

like image 607
Chris G. Avatar asked Feb 03 '23 19:02

Chris G.


1 Answers

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?

like image 157
icza Avatar answered May 16 '23 09:05

icza