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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ go mod edit -replace github.com/gobuffalo/packr=github.com/gobuffalo/packr@v2.8.0
$ cat go.mod
module github.com/adamveld12/riffraff

go 1.14

require (
        github.com/gobuffalo/packr v1.30.1
        github.com/golangci/golangci-lint v1.21.0 // indirect
        github.com/satori/go.uuid v1.2.0
)

replace 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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# clone your own copy and make some edits at ~/projects/packr
$ cd ~/projects && git clone https://github.com/gobuffalo/packr
$ cd ~/projects/riffraff && go mod edit -replace github.com/gobuffalo/packr=../packr
$ cat go.mod
module github.com/adamveld12/riffraff

go 1.14

require (
        github.com/gobuffalo/packr v1.30.1
        github.com/golangci/golangci-lint v1.21.0 // indirect
        github.com/satori/go.uuid v1.2.0
)

replace 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