Locally patching dependencies in Go

submited by
Style Pass
2024-07-04 14:30:05

In a previous post I talked about how each Go module is its own self-contained "virtual environment" during development. Among other benefits, this makes the dependencies of a module explicit and simple to tweak.

To use a concrete example, suppose our module depends on the popular package go-cmp, that lets us deep-compare arbitrary Go values. Say we're debugging an intricate scenario and want to either:

In a directory, run go mod init example.com (the module name is just a placeholder - it's a local experiment, we don't intend it to be imported or even published online). This creates a go.mod file; now, let's write this code:

And then run go mod tidy; this should get the github.com/google/go-cmp dependency, and the go.mod file will look something like:

Now, we'll download the dependency locally and patch it. Clone the https://github.com/google/go-cmp/ repository into a local directory; we'll call it $DEP (on my machine DEP=/home/eliben/test/go-cmp ). Next, edit $DEP/cmp/compare.go to add a log statement:

Leave a Comment