How to Use Govendor

Go version 1.11 introduces go module which is far better than govendor so if you use go version 1.11 or above, I recommend you to use go module instead. For version 1.5 to 1.10, go ahead with this article.

Govendor is a golang dependency management tool, like npm for node.js or pipenv for python. You can set the version of your dependencies and commit the config file in your repository then sit back and don’t have to worry that your code will break because of a package update in the future.

Getting Start

First of all, install govendor with go get.

go get -u github.com/kardianos/govendor
which govendor

If it can’t find where the binary file is, it means that you haven’t added $GOPATH/bin to $PATH. Add it with below command. You should also considering adding it to .bashrc, .zshrc or whatever shell setting you’re using.

export PATH=$PATH:$GOPATH/bin

After installation, go to your project directory. In this article I’ll use a simple file as below.

$ tree
.
└── main.go

0 directories, 1 file
$ cat main.go
package main

import (
        "fmt"

        "github.com/terngkub/gomod"
)

func main() {
        fmt.Println(gomod.Version())
}

In main.go, I imported my testing package which have version 1.0.0, 1.1.0 and 2.0.0. The package just have one function, Version(), which will return a version string of the package.

The next step is to initiate a vendor. This will create a vendor folder with vendor.json which is a config file.

$ govendor init
$ tree
.
├── main.go
└── vendor
    └── vendor.json

1 directory, 2 files
$ cat vendor/vendor.json
{
        "comment": "",
        "ignore": "test",
        "package": [],
        "rootPath": "how-to-use-govendor"
}

Then use govendor fetch. This will download the dependency to vendor folder and also add it to the config file. Note that it will get the latest commit (in this case version 2.0.0) if you don’t specify the version. Then you can use govendor list to list all the dependencies.

$ govendor fetch github.com/terngkub/gomod
$ ls vendor
github.com  vendor.json
$ cat vendor/vendor.json
{
        "comment": "",
        "ignore": "test",
        "package": [
                {
                        "checksumSHA1": "dMwMtHL+eS/zO0vbRSH+B1VV/zI=",
                        "path": "github.com/terngkub/gomod",
                        "revision": "62939fc9206131f15941c79c40d2aaedf14f9145",
                        "revisionTime": "2019-06-20T16:53:54Z"
                }
        ],
        "rootPath": "how-to-use-govendor"
}
$ govendor list
 v  github.com/terngkub/gomod    
pl  how-to-use-govendor          

After this you can build and golang will look for the package in vendor folder first. The result is v2.0.0 as expected.

$ go build -o how-to-use-govendor
$ ./how-to-use-govendor 
v2.0.0

To specify version, you can change it in vendor.json on versionExact field and use go sync to get a new one. As you can see, I change the version to 1.1.0 and it works! govendor list also show that it uses version 1.1.0.

$ cat vendor/vendor.json 
{
        "comment": "",
        "ignore": "test",
        "package": [
                {
                        "path": "github.com/terngkub/gomod",
                        "versionExact": "v1.1.0"
                }
        ],
        "rootPath": "how-to-use-govendor"
}
$ govendor sync
$ go build -o how-to-use-govendor
$ ./how-to-use-govendor 
v1.1.0
$ govendor list
 v  github.com/terngkub/gomod    v1.1.0
pl  how-to-use-govendor

You can now use govendor for some simple task. There is more to play with. You can find more information on govendor repository.

Leave a Reply

Your email address will not be published. Required fields are marked *