codingecho

日々の体験などを書いてます

IntelliJ IDEAを使ったGo言語のセットアップ

Go言語のlocalへのインストール方法からIntelliJ IDEAへのプロジェクトのインポートと設定について記述します。

Go言語のインストール

Go言語のインストールにはgoenvを使用します。

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

bashを使用している場合は以下のコマンドを実行して .bash_profile に設定を追加します。

$ echo 'export GOENV_ROOT="$HOME/.goenv"' >> ~/.bash_profile
$ echo 'export PATH="$GOENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(goenv init -)"' >> ~/.bash_profile

Go言語をインストールします。

$ goenv install 1.10.4
$ goenv local 1.10.4
$ go version
go version go1.10.4 darwin/amd64

InteliJ IDEAにGoプラグインをインストール

Configure -> Plugins をクリックします。

f:id:ytanak:20190114095856p:plain

Browse repositories をクリックします。

f:id:ytanak:20190113193457p:plain

Go プラグインをインストールします。

f:id:ytanak:20190113193504p:plain

インストールが完了したら、IntelliJ IDEAをリスタートします。

InteliJのプロジェクトのインポートとGoの設定

Import Project からプロジェクトを選択してを開きます。

f:id:ytanak:20190113193516p:plain

f:id:ytanak:20190113193616p:plain

Create project from existing sources にチェックして、 Next をクリックします。

f:id:ytanak:20190113193523p:plain

Next をクリックします。

f:id:ytanak:20190113193610p:plain

Next をクリックします。

f:id:ytanak:20190113193622p:plain

Finish をクリックします。

f:id:ytanak:20190113193530p:plain

これでプロジェクトのインポートが完了します。

IntelliJ IDEAのGo言語の設定

GOROOTとGOPATHの設定

GOROOTGOPATH を確認します。

$ go env
...
GOROOT="/Users/user_name/.goenv/versions/1.10.4"
GOPATH="/Users/user_name/go"
...

Languages & Frameworks -> Go -> GOROOT を開きます。

f:id:ytanak:20190113193545p:plain

GOROOTを指定します。パスを入力するにはShift + Cmd + Gでダイアログを表示することができます。

f:id:ytanak:20190113193537p:plain

Languages & Frameworks -> Go -> GOPATH を開きます。

f:id:ytanak:20190113193554p:plain

Project GOPATHGOPATH を指定します。パスを入力するにはShift + Cmd + Gでダイアログを表示することができます。

f:id:ytanak:20190113193601p:plain