How do I import a specific version of a package using go get?
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 [email protected]
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/[email protected]
$ go get github.com/wilk/[email protected]
But then, how can I make a difference during the import?
Go 1.11 will have a feature called go modules and you can simply add a dependency with a version. Follow these steps:
go mod init .
go mod edit -require github.com/wilk/[email protected]
go get -v -t ./...
go build
go install
Here's more info on that topic - https://github.com/golang/go/wiki/Modules
Really surprised nobody has mentioned gopkg.in.
gopkg.in
is a service that provides a wrapper (redirect) that lets you express versions as repo urls, without actually creating repos. E.g. gopkg.in/yaml.v1
vs gopkg.in/yaml.v2
, even though they both live at https://github.com/go-yaml/yaml
- gopkg.in/yaml.v1 redirects to https://github.com/go-yaml/yaml/tree/v1
- gopkg.in/yaml.v2 redirects to https://github.com/go-yaml/yaml/tree/v2
This isn't perfect if the author is not following proper versioning practices (by incrementing the version number when breaking backwards compatibility), but it does work with branches and tags.