The PHP ecosystem is quite spoiled with Composer for dependency management. Composer is very straightforward and easy to use. For the Golang ecosystem Glide is what Composer is for PHP; Glide is the easiest and (probably) most used dependency management tool for Golang.
This article compares Glide and Composer by four important Composer commands: init, install, require, and update. It does not cover installing Glide.
Glide also knows the “init” subcommand to initialise dependency management in your Golang project. This will set up the initial glide.yaml
for you after asking you some questions about the package you're setting up.
To create a glide.yaml
for your project:
$ glide init
Glide will then create the glide.yaml
file and it will also try to populate it. If you initialise glide on a codebase that already has dependencies mentioned in the .go source files.
Now that you know how to initialise glide in the project, it is time to install the dependencies. Composer and Glide are very similar in this aspect. To install dependencies simply call:
$ glide install
Glide will then install dependencies in the vendor/
folder, just as Composer does.
Glide’s “get” subcommand is the equivalent of Composer’s “require”. It also works much the same. Take the following two commands as an example:
$ composer require rtuin/somepackage
# Add a dev dependency $ composer require --dev rtuin/somepackage
# Or, with version specified $ composer require rtuin/somepackage:^1.0
Glide’s counterpart, note the #
instead of :
to denote a version:
$ glide get github.com/rtuin/somepackage
# Add a dev dependency $ glide get --test github.com/rtuin/somepackage
# Or, with version specified $ glide get github.com/rtuin/somepackage#^1.0
Glide respects the same way for denoting version numbers as Composer does.
Composer “update and Glide “update” have exactly the same results. It will find new dependency versions based on the packages listed in the glide.yaml
file and checks if these versions need to be installed based on the version range constraints you configured.
If any packages need to be updated Glide will, just like Composer, download this package to the vendor/
folder and write the new version to the glide.lock
file. In the end resulting in reproducible installations.
Glide uses the YAML format instead of json, but other than that the essence of the glide.yaml
file is similar to the composer.json
file.
Below you will see a composer.json
file and a glide.yaml
file to give you an idea of how similar they are.
There are three key differences:
glide.yaml
does not contain a version number. The package version is defined by the version control system you put your Glide-enabled package in. This can, for example, be a hash reference, revision number, or (semantic version) tag names.composer.json:
{ "name": "rtuin/somepackage", "description": "This is some package description", "homepage": "https://github.com/rtuin/somepackage", "license": "MIT", "version": "1.0.0", "authors": [ { "name": "Richard Tuin", "email": "[email protected]", "homepage": "http://www.rtuin.nl" } ], "autoload": { "psr-4": {"Rtuin\\Somepackage": "src/"} }, "require": { "rtuin/someotherpackage": "^4.1", }, "require-dev": { "rtuin/somedevdependency": "^5.3" }}
glide.yaml
package: github.com/rtuin/somepackagehomepage: https://github.com/rtuin/somepackagelicense: MITowners:- name: Richard Tuin email: [email protected] homepage: http://www.rtuin.nlimport:- package: github.com/rtuin/someotherpackage version: ^4.1testImport:- package: github.com/rtuin/somedevdependency version: ^5.3
As it turns out Golang’s Glide is very similar to PHP’s Composer. Glide is not the only popular dependency manager for Golang but it is definitely the easiest to use and it is very similar to Composer.
I’ve also made this content available in slides on slideshare:
Originally published at rtuin.nl.