18
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Go言語 - 開発環境構築

Last updated at Posted at 2019-07-28

開発環境

  • MacOS
  • Visual Studio Code
  • Github

達成目標

  • Golangのバージョン管理
  • Language Server
  • プロジェクト毎にパッケージ管理

gvmのインストール

Golangのバージョン管理ツール

メリット

  • Golangに必要なものを一箇所で管理してくれる ($HOME/.gvm内)
  • 複数のバージョンを gvm useコマンドで切り替えられる
gvmの設定手順
$ brew install mercurial

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

# 下のコマンドを.bashrcに設定する
$ source $HOME/.gvm/scripts/gvm

# .bashrc実行
$ source ~/.bashrc

Golangのインストール

この記事ではGolangのv1.12.7をインストールする手順を記す

Golangのインストール手順
# ダウンロードできる Golang のバージョンリスト
$ gvm listall

# v1.12.7をダウンロード
$ gvm install go1.12.7 -B

# v1.12.7をデフォルトで使う設定
$ gvm use go1.12.7 --default

ワークスペースの設定

Goを使った開発を行うディレクトリの事で、環境変数のGOPATHで指定する
この記事では、$HOME/goに設定する

# .bashrcに書く
export GOPATH=$HOME/go

# 設定できたか確認
$ source ~/.bashrc
$ go env | grep GOPATH

ワークスペースの階層構造

ワークスペースには3つのサブディレクトリが必要

  • bin: Goアプリのコンパイル後の実行ファイルを保管
  • pkg: 実行ファイルを作るための外部パッケージを保管
  • src: アプリ開発のソースコードを保管
$GOPATH ┬ bin
        ├ pkg
        └ src ─ github.com ─ {github_id} - {repository_name} ┬ .git
                                                             └ main.go
  • {github_id}はGithubのアカウント名
  • {repository_name}はリポジトリの名前

Language Serverの設定

公式でオススメされているLanguage Serverはgopls

その設定手順を記す。

1. GolangのVSCodeの拡張機能であるGoを入れる

2. goplsをインストールする

$ git clone -b bingo https://github.com/saibing/tools.git $GOPATH/tools
$ cd $GOPATH/tools/gopls
$ go install

3. VSCodeのsettings.jsonに下の内容を追加後、VSCodeを再起動

settings.json
    "go.useLanguageServer": true,
    "go.languageServerExperimentalFeatures": {
        "format": true,
        "autoComplete": true,
        "rename": true,
        "goToDefinition": true,
        "hover": true,
        "signatureHelp": true,
        "goToTypeDefinition": true,
        "goToImplementation": true,
        "documentSymbols": true,
        "workspaceSymbols": true,
        "findReferences": true,
        "diagnostics": true
    },
    "[go]": {
        "editor.snippetSuggestions": "none",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        },
    },
    "go.toolsEnvVars": {
        "GO111MODULE": "on"
    },
    "files.eol": "\n",

パッケージ管理ツール

Golangのv1.11以降で採用されているgo modules(vgo)を用いる

vgoを使う宣言
# .bashrcに書く
export GO111MODULE=on

# 設定できたか確認
$ source ~/.bashrc
$ go env | grep GO111MODULE

go.modというパッケージ管理ファイルを作っておけば、go buildを実行したときに、自動的に依存関係を理解してインストールして実行し、go.modに記録してくれる。

上で説明した階層構造の場合は以下の手順でgo.modを作成する。

$ cd $GOPATH/src/github.com/{github_id}/{repositoty_name}
$ go mod init github.com/{github_id}/{repository_name}

参考文献

18
32
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
18
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?