Help us understand the problem. What is going on with this article?

MacにIntelliJ IDEAでGolangの開発環境を構築する

More than 5 years have passed since last update.

IntelliJ IDEAとHomebrewでインストールしたGolangで開発環境を構築した時にハマったのでメモ。

環境

GolangをHomebrewでインストール

下記コマンドでGolangをインストール

Terminal
$ brew install go
$ go version
go version go1.3 darwin/amd64

環境変数にGOROOTとGOPATHを追加

.bash_profileもしくは.bashrcGOROOTGOPATH を追加します。
GOROOT に設定するパスは/usr/local/opt/goではなくlibexecまで指定する必要があります。(ここでハマりました)

.bash_profile
export GOROOT=/usr/local/opt/go/libexec
export GOPATH=$HOME/.go

ここを参考に/etc/launchd.confにも同じ値を追加します。
/etc/launchd.confが存在しない場合は、新規作成してください。

launchd.conf
setenv GOROOT /usr/local/opt/go/libexec
setenv GOPATH $HOME/.go

最後に下記コマンドで環境変数を反映します。

Terminal
$ source ~/.bash_profile
$ launchctl < /etc/launchd.conf
$ echo $GOROOT
/usr/local/opt/go/libexec
$ echo $GOPATH
/Users/Yourname/.go

IntelliJ IDEAにGolang環境を構築

  • IntelliJ IDEAのPluginsでGo language support pluginをインストール&再起動
  • [Create New Project]でGoのプロジェクトを作成し、SDK選択画面で[Configure...]からGOROOTを選択
  • プロジェクト作成後にGOROOTGOPATHに関するエラーが表示されなければ構築完了

intellij_golang.png

備考

PhpStormでも同様の手順で環境構築ができます。

謝辞

@abe00makoto さんGOROOTの指定についてご指摘いただき、ありがとうございます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away