Skip to content
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

258 lines (187 sloc) 5.18 KB


gohack is a tool that helps you make temporary edits to your Go module's dependencies. By default, dependencies are stored in a read-only cache. gohack adds a replace directive to directory where you can "hack" the dependency's code, and then removes the directive when you are done. For more details on replace directives, see the Go modules wiki.

This example shows how to use gohack.


Install gohack:

$ gobin
Installed to /home/gopher/bin/gohack

Create a module:

$ mkdir -p /home/gopher/scratchpad/using-gohack
$ cd /home/gopher/scratchpad/using-gohack
$ go mod init
go: creating new go.mod: module

Add a simple dependency to a main package in our module:

$ cat blah.go
package main

import (

func main() {

In this case, we use a specific version of our dependency:

$ go get
go: finding v1.5.1
go: finding v1.3.0
go: finding v0.0.0-20170915032832-14c0d48ead0c
go: downloading v1.5.1
go: extracting v1.5.1
go: downloading v1.3.0
go: extracting v1.3.0
go: downloading v0.0.0-20170915032832-14c0d48ead0c
go: extracting v0.0.0-20170915032832-14c0d48ead0c

Run as a quick "test":

$ go run .
Hello, world.

Now let's assume we want to tweak the package, in particular we want to tweak the Hello function we are using. We use gohack to do this.

"Hack" on

$ gohack get => /home/gopher/gohack/

Verify our module is using the local "hack" copy:

$ go mod edit -json
	"Module": {
		"Path": ""
	"Go": "1.12",
	"Require": [
			"Path": "",
			"Version": "v1.5.1"
	"Exclude": null,
	"Replace": [
			"Old": {
				"Path": ""
			"New": {
				"Path": "/home/gopher/gohack/"

gohack puts "hacks" in $HOME/$module; we make our changes there:

$ cd /home/gopher/gohack/
$ cat <<EOD >quote.go
package quote

func Hello() string {
	return "My hello"

Rerun our example:

$ cd /home/gopher/scratchpad/using-gohack
$ go run .
My hello

Revert to the original

$ gohack undo
$ go run .
Hello, world.

More specific information about subcommands is available via help, for example:

$ gohack help get
usage: get [-vcs] [-u] [-f] [module...]

The get command checks out Go module dependencies
into a directory where they can be edited.

It uses $HOME/gohack/&lt;module&gt; as the destination directory.
(TODO implement directory overriding)

By default it copies module source code from the existing
source directory in $GOPATH/pkg/mod. If the -vcs
flag is specified, it also checks out the version control information into that
directory and updates it to the expected version. If the directory
already exists, it will be updated in place.

Version details

go version go1.12.5 linux/amd64 v1.0.0
You can’t perform that action at this time.