Over a million developers have joined DZone.

Forking With Go

Forking and pulling changes into Go projects works a little differently than in languages like JavaScript. Here's how to do it.

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Making contributions to the open source community is fairly easy, as least when it comes to most programming languages. Sadly (or not?), Go is an unusual language and it has to be handled specifically here. Let's take a look on forking and pulling changes into Go projects.

Why Is It Different?

Go stands out from the field in terms of defining relations between packages (called modules in some languages). Unlike, for example, Java Script, all paths to the imports are absolute and not relative. How does it make any difference?

Imagine having a couple of files:

// packageA/fileA.js
module.exports = {
    shout: function() {

// packageB/fileB.js
var a = require('../packageA/fileA');

Wherever you check such repo out, you can make a change to fileA and see its effect immediately in the other one. With Go, the code is stored in an arbitrary location in your operating system ($GOPATH) and we cannot (easily) have the same project checked out twice. Another problem is that all paths in Go imports are absolute:

// packageA/fileA.go
package packageA

import "fmt"

func Shout() {

// packageB/fileB.go
package packageB

import "github.com/userZ/projectX/packageA"


If you want to fork the project from GitHub (userZ/projectX) to your own account (eg. myself/projectX), this will mess up all imports. Do we have to change them back and forth between pull requests? Not at all!

Smart Workaround

Unfortunately, Go requires us to do a small trick in order to work on a forked repository. We can use neither

cd $GOPATH/src/github.com/myself
git clone https://github.com/myself/projectX.git


go get github.com/myself/projectX

Instead, we have to trick Go into believing that our newly forked repo is under an original path. First, we need to remove the original one ($GOPATH/src/github.com/userZ/projectX) and clone the fork right in that place:

cd $GOPATH/src/github.com/userZ
git clone https://github.com/myself/projectX.git

How does it work? You now can edit the repository as you like, and whenever you import an original path in one of the source files, Go will actually look on your fork.

Might sound a bit confusing at first, but it is really not that hard. Give it a try!

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

golang ,github ,forking workflow

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}