DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > Forking With Go

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.

[deleted] user avatar by
[deleted]
·
Jun. 20, 16 · DevOps Zone · Tutorial
Like (5)
Save
Tweet
2.38K Views

Join the DZone community and get the full member experience.

Join For Free

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() {
        console.log('Aaaaaaaa!');
    }
};

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

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() {
  fmt.Println("Aaaaaaaa!")
}


// packageB/fileB.go
package packageB

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

packageA.Shout()

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

nor

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!

operating system Open source Fork (software development) Repository (version control) GitHub Requests Forth (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java
  • A Simple Guide to Heaps, Stacks, References, and Values in JavaScript
  • Unit vs Integration Testing: What's the Difference?
  • Adaptive Change Management: A DevOps Approach to Change Management

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo