paint-brush
Golang for PHP Developers: Dependency managementby@rtuin
434 reads
434 reads

Golang for PHP Developers: Dependency management

by Richard TuinJune 2nd, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The <a href="https://hackernoon.com/tagged/php" target="_blank">PHP</a> ecosystem is quite spoiled with <a href="https://getcomposer.org/" target="_blank">Composer</a> for dependency management. Composer is very straightforward and easy to use. For the <a href="https://hackernoon.com/tagged/golang" target="_blank">Golang</a> ecosystem <a href="http://glide.sh/" target="_blank">Glide</a> is what Composer is for PHP; Glide is the easiest and (probably) most used dependency management tool for Golang.

People Mentioned

Mention Thumbnail

Company Mentioned

Mention Thumbnail
featured image - Golang for PHP Developers: Dependency management
Richard Tuin HackerNoon profile picture

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.

composer init for 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.

composer install for Glide

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.

composer require for Glide

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 for Glide

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’s composer.json

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:

  • The package definition in 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.
  • There is no “dev” import, but “test” imports. (But you can use them in the same way.)
  • The autoload directive is not present in Glide. This is because Golang has it’s own fixed way of loading dependencies and you can’t influence them using Glide. Package loading is predictable in Golang and needs no configuration value in the dependency manager.

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

Conclusion

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.