Go Modules: How to replace a dependency with a local copy

golang, modules

Lets say you’re working on a go package and you need to replace a dependency with a specific version for testing. You can use the awesome go mod edit -replace old=new[@version] command to do this.

This command adds a replace directive to your go.mod that overrides any require statement versions for the matching module:

 1$ go mod edit -replace github.com/gobuffalo/packr=github.com/gobuffalo/[email protected]
 2$ cat go.mod
 3module github.com/adamveld12/riffraff
 5go 1.14
 7require (
 8        github.com/gobuffalo/packr v1.30.1
 9        github.com/golangci/golangci-lint v1.21.0 // indirect
10        github.com/satori/go.uuid v1.2.0
13replace github.com/gobuffalo/packr => github.com/gobuffalo/packr v2.8.0 

Now when you build your go app, it will use v2.8.0 of packr in place of the version specified in the require block.

But what if you want to use it similarly to npm link, where you want to replace a module with a local working copy?

Run the same command but omit the @version on the new package like so:

 1# clone your own copy and make some edits at ~/projects/packr
 2$ cd ~/projects && git clone https://github.com/gobuffalo/packr
 3$ cd ~/projects/riffraff && go mod edit -replace github.com/gobuffalo/packr=../packr
 4$ cat go.mod
 5module github.com/adamveld12/riffraff
 7go 1.14
 9require (
10        github.com/gobuffalo/packr v1.30.1
11        github.com/golangci/golangci-lint v1.21.0 // indirect
12        github.com/satori/go.uuid v1.2.0
15replace github.com/gobuffalo/packr => ../packr #now points at your local copy

When you’re all finished up you can remove the replace directive with the following command:

1$ go mod edit -dropreplace github.com/gobuffalo/packr

Articles from blogs I follow

My plans at FOSDEM: SourceHut, Hare, and Helios

FOSDEM is right around the corner, and finally in person after long years of dealing with COVID. I’ll be there again this year, and I’m looking forward to it! I have four slots on the schedule (wow! Thanks for arranging these, FOSDEM team) and I’ll be talkin…

via Drew DeVault's blog January 24, 2023

YSK: Google allows spoofing news headlines in search results

A minor scandal unfolding in the Swedish election highlights a way to influence news narratives: Google allows you to set headlines for news articles in search results by paying for adwords placements of legitimate articles. This is being used by political …

via Jacob Davis-Hansson September 9, 2022

Going multipath without Multipath TCP

Going multipath without Multipath TCP Gigabit ethernet has been around for a long time, it’s so ubiquitous that there is a very strong chance that if you have a RJ-45 port on your compu

via benjojo blog February 24, 2022

Generated by openring