LoginSignup
5
3

More than 1 year has passed since last update.

goenvでGolangのバージョン管理(最新版のGolangを使う)

Last updated at Posted at 2020-02-29

golang を使った開発のため、バージョン管理を導入したときの手順を書きます。

goenv について

goenv は Golang のバージョン管理ツールです。

pyenv や rbenv など、他のバージョン管理ツールと同じようなものです。

環境情報

  • macOS Catalina 10.15.3
  • zsh 5.7.1 (x86_64-apple-darwin19.0)

goenv インストール

goenv のソースをリポジトリから取得する。

zsh
$ git clone https://github.com/syndbg/goenv.git ~/.goenv

次は bash の場合 .bashrc、zsh の場合 .zshrc の設定をしていきます。

goenv パス設定

goenv を使うためのパスを通します。

.zshrc

$ vim ~/.zshrc で編集開始。

.zshrc
...
export GOENV_ROOT=$HOME/.goenv
export PATH=$GOENV_ROOT/bin:$PATH
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"
...

GOPATH 設定

go を使う時に必要な $GOPATH を設定しておきます。

.zshrc

goenv のパス設定のあとに追記します。

$ vim ~/.zshrc で編集開始。

.zshrc
...
export GOPATH=$HOME/go
...

設定すると、$HOME/go 以下にパッケージ等がインストールされていくようになります。
(go get -u github.com/go-sql-driver/mysql したものとか。)

設定反映

zsh
$ exec $SHELL

Go インストール

$ goenv install -l
Available versions:
  1.2.2
  1.3.0
  1.3.1
  ...
  1.11.4

...

$ goenv install 1.11.4
...
Installed Go Darwin 64bit 1.11.4 to /path/to/.goenv/versions/1.11.4

利用する Go バージョンの指定

環境によってバージョン切り替えます

# global の設定
$ goenv global 1.11.4

# 特定リポジトリ以下のバージョンを指定する場合
$ cd /path/to/repository
$ goenv local 1.9.7

これで、好きな場所、好きなバージョンで go を使えます。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3