Search code examples
gogo-modules

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.

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


Solution

  • 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?