Forking GoLang modules

Go uses path names in its import statements. When you fork a module and try to build it, the build will fail. For example, I want to add some functionality to go-echarts. I fork it at github, and then on my Macbook Pro, I clone the fork:

$ git clone git@github.com:macfisherman/go-echarts.git
Cloning into 'go-echarts'...
Warning: Permanently added the RSA host key for IP address '140.82.113.3' to the list of known hosts.
remote: Enumerating objects: 2181, done.
remote: Total 2181 (delta 0), reused 0 (delta 0), pack-reused 2181
Receiving objects: 100% (2181/2181), 1.75 MiB | 6.39 MiB/s, done.
Resolving deltas: 100% (1416/1416), done.

I then want to see if it builds.

jeffs-mbp:projects jeff$ cd go-echarts
jeffs-mbp:go-echarts jeff$ cd charts/
jeffs-mbp:charts jeff$ go build .
geo.go:9:2: cannot find package "github.com/go-echarts/go-echarts/datasets" in any of:
	/usr/local/go/src/github.com/go-echarts/go-echarts/datasets (from $GOROOT)
	/Users/jeff/go/src/github.com/go-echarts/go-echarts/datasets (from $GOPATH)
base.go:4:2: cannot find package "github.com/go-echarts/go-echarts/datatypes" in any of:
	/usr/local/go/src/github.com/go-echarts/go-echarts/datatypes (from $GOROOT)
	/Users/jeff/go/src/github.com/go-echarts/go-echarts/datatypes (from $GOPATH)
engine.go:13:2: cannot find package "github.com/go-echarts/go-echarts/templates" in any of:
	/usr/local/go/src/github.com/go-echarts/go-echarts/templates (from $GOROOT)
	/Users/jeff/go/src/github.com/go-echarts/go-echarts/templates (from $GOPATH)

In this case there is no go.mod.

jeffs-mbp:charts jeff$ cd ..
jeffs-mbp:go-echarts jeff$ ls go.mod
ls: go.mod: No such file or directory

By adding one, building will work:

jeffs-mbp:go-echarts jeff$ go mod init github.com/go-echarts/go-echarts
go: creating new go.mod: module github.com/go-echarts/go-echarts
jeffs-mbp:go-echarts jeff$ cd charts/
jeffs-mbp:charts jeff$ go build .
go: finding module for package github.com/gobuffalo/packr
go: found github.com/gobuffalo/packr in github.com/gobuffalo/packr v1.30.1

I can now use this local fork in a project that needs the modification the fork has by adding a line to go.mod (the project already has a go.mod)

jeffs-mbp:charts$ cd ~/projects/covid-19
jeffs-mbp:covid-19 jeff$ go mod edit -replace="github.com/go-echarts/go-echarts => ../go-echarts"
jeffs-mbp:covid-19 jeff$ go list -m all | grep go-echarts
github.com/go-echarts/go-echarts v0.0.0-20190915064101-cbb3b43ade5d => ../go-echarts

The key is to use a recent Go. I’m using 1.14.2, but I believe any version since 1.11 will work too.