Naser Mirzaei

Using Go Modules

What’s This?

Go 1.11 includes preliminary support for versioned modules. Modules are an experimental opt-in feature in Go 1.11, with the hope of incorporating feedback and finalizing the feature for Go 1.12. Now we want to use this feature as simple as possible.

Let’s go!

First of all create a new directory everywhere you want, even out of your $GOPATH.

mkdir hello-world
cd hello-world

Now, you can initialize you go project with:

go mod init

Use your own repo url for the module.

If you are in $GOPATH you will get this message:

go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'

You can get rid of this message by changing this environment variable to on:

export GO111MODULE=on

A go.mod file created in your repository.

cat go.mod



Now start coding!

For sample code I use Gin’s Quick Start sample:

package main

import ""

func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
	r.Run() // listen and serve on

Save this code as main.go in your project.

It’s Time to Show Off

You don’t need to do extra works, just build your project:

go build

Go will start to find dependencies and builds your project

go: finding latest
go: finding latest
go: finding latest
go: downloading v0.0.0-20181209151446-772ced7fd4c2

Build completed, check your project directory again:


new files added:

go.mod // changed, check it.
go.sum // dependencies lock file
hello-world // project binary

Go fetched you project dependencies and added them to go.mod file:


require ( v0.0.0-20170109093832-22d885f9ecc7 // indirect v1.3.0 v1.2.0 // indirect v0.0.4 // indirect v0.0.0-20181209151446-772ced7fd4c2 // indirect v8.18.2 // indirect v2.2.2 // indirect

You can see all direct and indirect dependencies. indirect means that this dependency doesn’t use directly in your code, and uses by your direct dependencies.

go.sum file stores your dependencies commit hash and version tag: v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY= v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY+LsWmuwob+CRS1BmdRdjphAm9mH4= v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

It’s almost done.

There is some extra commands to know:

  • use go get -u to update all dependencies to latest minor version.
  • use go get -u=patch to update all dependencies to latest patch version.
  • go mod tidy prunes any no-longer-needed dependencies from go.mod and adds any dependencies needed for other combinations of OS, architecture, and build tags.

At the end you can check the directory that go stores modules:

cd $GOPATH/pkg/mod

Every module stored with multiple versions, for example:[email protected]

I have only one version of gin in my mod path.

Next time you want to get a dependency, go first checks mods directory, if the specific version not found, will download that. So you can cache this directory in CI servers or code.

Golang Go Modules Programming Dependency Management