Go言語をIntelliJで環境構築する【2021年度】

  • 2021年10月5日
  • 2022年10月18日
  • Go, backend
  • 40view
Go

Go言語を最近触り始めたので、IntelliJでの環境構築についてまとめました。

Goをインストール

まずはGoをインストールする。

https://github.com/gunners6518/golang-study
ここからインストール。
インストール後はgo versionでバージョンが表示されればOK。

ちなみに2021年10月は以下が最新バージョンです。

❯ go version
go version go1.17.1 darwin/amd64

GOROOTとGOPATHを設定

次にGoをローカル環境で使えるように各種の設定をしていきます。

IntelliJにGoのプラグインを追加。

Preferences/プラグイン/Goをインストールして再起動。

GOROOTを設定

Preferences/言語&フレームワーク/GOROOTを開いてSDKを設定。

GOROOTは以下のコマンドで確認

GOPATHを設定

Preferences/言語&フレームワーク/GOPATHを開いて、グローバル、プロジェクトPATHを設定。

~/.zshenvに記載

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

これにて環境構築は終了です。

コマンドを走らせる

サンプルファイルを作ってコマンドが走るか確認してみましょう。

mkdir -p src

srcディレクトリ配下にmain.goを作る。

package main
import "fmt"
func main() {
    fmt.Print("Hello World")
}

これで上手くいけば”Hello World”が出力される。

❯ go run src/main.go
Hello World%    

以上。

参考

https://tome123.com/post-376/ https://www.jetbrains.com/help/idea/configuring-goroot-and-gopath.html#gopath

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the compare bar
Compare
Compare ×
Let's Compare! Continue shopping