Skip to content

Using “replace” in go.mod to point to your local module

If you want to say, point to the local version of a dependency in Go rather than the one over the web, use the replace keyword.

The replace line goes above your require statements, like so:


replace => /Users/pselle/Projects/bar

require ( v1.0.0

And now when you compile this module (go install), it will use your local code rather than the other dependency.

According to the docs, you do need to make sure that the code you’re pointing to also has a go.mod file:

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. If the go.mod file is not present, you can create one with go mod init.

You can also create this line from the command line using the go mod edit

$ go mod edit -replace

Following the -replace is first what you want to replace, then an equals sign, then what you’re replacing it with.

Hopefully this helps someone else get a quick answer to “how do I do this” in the future 🙂

23 Replies to “Using “replace” in go.mod to point to your local module”

  1. Thanks for sharing. In my case, after first build all the files in my forked directory becomes read-only. Is it normal?

    1. Strange! I’ve never heard of that! If you ask about this on Stack Overflow, could you link here? I’m really interested in the answer to this. I’m sure your OS and what version of Go you’re using are relevant to this question.

  2. Thanks for the post, I have a multiple module in same repo with inter-dependency.

    Here is the sample of the go.mod after adding replace directive


    go 1.14

    require ( v0.0.0-20200408045958-1c52fa7f06ec v0.0.0-20200501252115-35b4806c1424

    replace => ../modules

    GO build works fine, but how to get rid of pseudo versions of modulea & moduleb specified here. I mean instead of specific git commit (pseudo version), I want it to use the local directory (../modules/)

    Thanks in Advance

  3. Thanks, it works. But when I want to upgrade the packages using “go get -u”, go said

    “go get: upgrading some/path@v1.0.0: unrecognized import path “some/path”: https fetch: Get “https://some/path?go-get=1″: dial tcp i/o timeout”

    How to solved that?

  4. I’m trying to convert my local go projects to go modules after upgraded to `go 1.16`. This line
    According to the docs, you do need to make sure that the code you’re pointing to also has a go.mod file
    saved me after many many other attempts. Thanks.

  5. Thank you for this! This did indeed help after a “how do I do this” search from a Google search! I have to say that it was so refreshing to find a blog post that stated the problem and demonstrated the solution without using so many paragraphs. Subscribed!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.