coming from a Node
environment I used to install a specific version of a vendor lib into the project folder (node_modules
) by telling npm
to install that version of that lib from the package.json
or even directly from the console, like so:
$ npm install express@4.0.0
Then I used to import that version of that package in my project just with:
var express = require('express');
Now, I want to do the same thing with go
. How can I do that?
Is it possible to install a specific version of a package? If so, using a centralized $GOPATH
, how can I import one version instead of another?
I would do something like this:
$ go get github.com/wilk/uuid@0.0.1
$ go get github.com/wilk/uuid@0.0.2
But then, how can I make a difference during the import?
Go 1.11 will be having a feature called go modules and you can simple add a dependency with a version.
Steps
go mod init .
go mod edit -require github.com/wilk/uuid@0.0.1
Here's more info on that topic: https://github.com/golang/go/wiki/Modules
Glide is a really elegant package management for Go especially if you come from Node's npm or Rust's cargo.
It behaves closely to Godep's new vendor feature in 1.6 but is way more easier. Your dependencies and versions are "locked" inside your projectdir/vendor directory without relying on GOPATH.
Install with brew (OS X)
Init the glide.yaml file (akin to package.json). This also grabs the existing imported packages in your project from GOPATH and copy then to the project's vendor/ directory.
Get new packages
Update and lock the packages' versions. This creates glide.lock file in your project directory to lock the versions.
I tried glide and been happily using it for my current project.
From Go 1.5 there's the "vendor experiment" that helps you manage dependencies. As of Go 1.6 this is no longer an experiment. Theres also some other options on the Go wiki..
Edit: as mentioned in this answer gopkg.in is a good option for pinning github-depdencies pre-1.5.