St_Hakky’s blog

Data Science / Human Resources / Web Applicationについて書きます

【Golang】Go自体の環境のバージョン管理ができるgvmの導入

こんにちは。

会社でGoを本格的に使う感じになりそうなので、色々手探りながら勉強しながらやっています。

今回は、Go自体のバージョン管理を行うことができる、gvmを使ってみました。

gvmとは

gvm は、Go Version Manager の略で、

github.com

インストール

今回は、Macへのインストールをしていきます。基本は、README.mdに書いてある通りにやればオッケーです。

依存するライブラリのインストール

まず、以下のコマンドを叩いて、依存ライブラリをインストールします。

$ xcode-select --install
$ brew update
$ brew install mercurial
gvmのインストール

私は、zshを使っているので、以下のコマンドですが、bashを使っている方は、 `zsh` ってなっているところを、 `bash` ってしてください。

$ zsh < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

これでインストール自体は完了です。

gvmのざっくりした使い方

helpを眺めてみます。

$ gvm help
Usage: gvm [command]

Description:
  GVM is the Go Version Manager

Commands:
  version    - print the gvm version number
  get        - gets the latest code (for debugging)
  use        - select a go version to use (--default to set permanently)
  diff       - view changes to Go root
  help       - display this usage text
  implode    - completely remove gvm
  install    - install go versions
  uninstall  - uninstall go versions
  cross      - install go cross compilers
  linkthis   - link this directory into GOPATH
  list       - list installed go versions
  listall    - list available versions
  alias      - manage go version aliases
  pkgset     - manage go packages sets
  pkgenv     - edit the environment for a package set

ざっくりなんとなくわかった(適当)

goをインストールしてみる

今回は特に指定がないので、インストール可能なバージョンを見てみます。

$ gvm listall

上のコマンドを叩くとめっちゃ出てきますが、今回は1.13.5をインストールしてみようと思います。

$ gvm install --help
Invalid version: --help
Usage: gvm install [version] [options]
    -s,  --source=SOURCE      Install Go from specified source.
    -n,  --name=NAME          Override the default name for this version.
    -pb, --with-protobuf      Install Go protocol buffers.
    -b,  --with-build-tools   Install package build tools.
    -B,  --binary             Only install from binary.
         --prefer-binary      Attempt a binary install, falling back to source.
    -h,  --help               Display this message.

ヘルプによると、普通にversionを指定すればよさそうっすね。

ただ、 README.md によると、`Go 1.5+` をインストールするには、 `Go 1.4` のインストールがまず必要みたいですが、 `-B` オプションをつけて、バイナリからインストールしちゃいましょう。

$ gvm install go1.13.5 -B

これで完了っす。