Create projects independent of $GOPATH using Go Modules

Yashish Dua
Mar 19, 2019 · 4 min read
Image for post
Image for post

If you are developing in Go, long before the release of Go 1.11, you must have a terrible experience as I had to deal with $GOPATH. For those who don’t know or haven’t come through this, before Go 1.11 all projects had to be created inside the $GOPATH. It is used by the Go compiler to search for dependencies when building your Go application. $GOPATH contains source code and binaries.

$GOPATH contains three high-level directories —
1. $GOPATH/src — Your all code resides here
2. $GOPATH/pkg — Packages used by Go internally to run itself
3. $GOPATH/bin — Contains executable binaries like godep, golint

With the latest release of Go 1.11, Go Modules are introduced which let you create your project outside the $GOPATH and improves package management a lot.

Shortcomings before Go Modules?

What exactly is a Go Module?

A module is a collection of related Go packages that are versioned together as a single unit. Modules record precise dependency requirements and create reproducible builds.
~ Official Go documentation


Create a .go file like this anywhere you like outside GOPATH and try to add it in a new folder.

Image for post
Image for post

This go project uses two external go packages and I haven’t go get it. I have just directly added their path in the import.

$ go mod init{your_username}/{repo_name}

This will create a go.mod in your root which would look like this —

Image for post
Image for post

$ go build

Go build command will do all the work for you! Once you run this, it will automatically detect imports of your project and will add it to your go.mod file. This file is just like a package.json but better than that ;)
You can even manually edit the imports here.

Image for post
Image for post

The import here uses semantic versioning:

It also creates one more file named go.sum , this contains the cryptographic checksums and it’s not a lock file. It provides security and would most of the time contains more packages than go.mod since it contains every revision.

Note: go.mod and go.sum, both needs to be added to version control.

$ ./gomod

Image for post
Image for post

Easter Eggs


Our community publishes stories worth reading on Android…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store