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:
module github.com/pselle/foo
replace github.com/pselle/bar => /Users/pselle/Projects/bar
require (
github.com/pselle/bar 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
https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directivereplace
directive is a filesystem path, then the target must have ago.mod
file at that location. If thego.mod
file is not present, you can create one withgo mod init
.
You can also create this line from the command line using the go mod edit
$ go mod edit -replace github.com/pselle/bar=/Users/pselle/Projects/bar
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 🙂
Just what I needed! Thanks
simple and useful.
thanks!
Thanks Pam. This is exactly what I needed to do some private development work.
U just save my day!! Go Dev Girls!!!
Sweeeeet, thanks!
Thank you!!!! It was very helpful your explanation post.
Cheers from Costa Rica
Thank you
Thanks for sharing. In my case, after first build all the files in my forked directory becomes read-only. Is it normal?
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.
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
module mycompany.com/project/repo/modules/modulec
go 1.14
require (
mycompany.com/project/repo/modules/modulea v0.0.0-20200408045958-1c52fa7f06ec
mycompany.com/project/repo/modules/moduleb v0.0.0-20200501252115-35b4806c1424
)
replace mycompany.com/project/repo/modules => ../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
Very nice article. Just one thing I would like to point out: Perhaps you could call out that replace can work with VCS repository as well not just local file system.
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 172.120.132.3:443: i/o timeout”
How to solved that?
One assumes that 172.120.132.3 is a server somewhere inside your private network, is that so? Does it actually reply to anything on port 443? Or maybe that’s your own computer/desktop, and you’re running a copy of some sort of
git
repository there? That’s because the IP address you’ve listed in your comment is a private address — no Internet routing to it! — and as you’ve redacted the actual path you’re using, there is no way to know if you’re actually typing things correctly…Thanks for the note it works for me. We just move from Github to Bitbucket and I need to re-build CI/CD
Thanks a lot Pam.
Cheers!
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.
Thank you so much! This really helped me a lot to figure out a patch to improve a library my program was using <3
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!
You saved my day. Thanks
This worked beautifully. Thanks alot Pam.
Is there a way to undo this replace via command line?
Thanks! This is exactly what I was looking for
Thanks, exactly what I was looking for.
Thanks! Saved me a lot of time!
Thanks, Pam. This saved my day. I wanted to apply a patch to the go module and this helped me to point to the patched version. Thank you.
Just what I needed. Thanks!
thanks for sharing this, very help for newbie like me
hello
tks for the post, I’m having an issue with how to update the cache of my library
I added the replace instruction correctly and is pointing to the local path, but I made some changes in that library, and the changes are not reflected in the client that is using the library, I tried several things lake go mod tidy, go get -u, go build and is not updating with the new changes
any help on this?
Hi there! I just wished to add my thank-you note here. I keep forgetting the order of the
replace
command; and while it ought to be obvious, that tip about having a validgo.mod
inside the replacement module is something so easily forgotten… so thanks for reminding us to check for it as well!This is brilliant, thanks for posting this
the replace line, should be right after the require section, it works for me!
Thank you Pam for share this message
Hi Pam, thanks for this post. Do you have to check in the go.mod with the edit in place or is it designed just for local work? The reason I ask, is that I have had so many problems when I attempt to use this feature. My next task is to move a package in a git repo to a different location in the same module, and I am anticipating lots of problems due to not knowing how to use go mod edit properly.
Thanks Pam, this was the quickest and most straightforward answer. Appreciate your work!