LoginSignup
22
13

More than 3 years have passed since last update.

golang + vscode + bingo(gopls) でコード補完を実現する

Posted at

ある日突然コード補完が効かなくなり調べていると、
gocode やめます(そして Language Server へ)
を見つけました。

コード補完が無いと辛いので、bingo(gopls) を導入しました。
本記事はその備忘録です。

前提条件

  • macOS High Sierra
  • fish
  • $GOPATH 以下で作業しています。
  • Go 1.12 以上
    • brew & goenv を使用している場合 1.12beta までしか対応していません。(2019/05/23時点)
      brew install(upgrade) go で直接インストールし、goenv では system を選択しました。
  • vscode と https://github.com/Microsoft/vscode-go はインストール済みです。
    • > Go: Install/Update Tools で各ツールもインストール済みです。
      元の gopls もインストールされますが、後ほど bingo に置き換えます。

環境変数の確認

fish の例です。
1行目は goenv の設定です。使用していない人は不要です。
Go1.13 以降で GO111MODULE=on がデフォルトになった場合は最終行も不要です。

config.fish
status --is-interactive; and source (goenv init -|psub)
set -x GOPATH ~/go
set -x PATH $GOPATH/bin $PATH
set -x GO111MODULE on

bingo(gopls) のインストール

https://github.com/saibing/tools#install に記載されているインストールコマンドを実行します。
go install すると、vscode 上からインストールした $GOPATH/bin/gopls が入れ替わります。

vscode の設定

settings.json に以下の設定を追加します。

"gopls" を設定すると、vscode のエラーが出ますが、特に問題ないそうです :sweat_drops:

VSCode will complain but it should still work. Once we have a stable set of configs for gopls, we will merge them with the VSCode extension so that you don't get that error message.

そのうち修正されるようです。

動作確認

vscode を再起動してください。
> Developer: Reload Window ではうまくいかない?ことがありそう?です。

image.png

このようにコード補完されます :smiley:

動作が少し重いので、適時 > Developer: Reload Window してください。
また、「出力」でエラーなども確認できます。gopls を選択してください。

image.png

hover 時にエラーが出ることがあります。

"Request textDocument/definition failed" エラーが出る場合

自身で作成したコードの補完が効かない事があります。
https://github.com/golang/go/issues/31492

リポジトリのトップに go.mod がないのが原因?のようです。
.code-workspace ファイルを設置して folders を設定します。

VS Codeのワークスペース」を参考にしました。

参考記事

22
13
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
22
13