You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.8 KiB
58 lines
1.8 KiB
---
|
|
title: "Go Modules: How to replace a dependency with a local copy"
|
|
date: 2020-03-20T20:35:25Z
|
|
draft: false
|
|
tags: [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:
|
|
|
|
```bash {hl_lines=[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:
|
|
|
|
```bash {hl_lines=[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:
|
|
|
|
```bash
|
|
$ go mod edit -dropreplace github.com/gobuffalo/packr
|
|
```
|