LoginSignup
2
1

More than 1 year has passed since last update.

VSCodeのGoの拡張機能でエラーがでる (DockerでGoをやるうえでの注意)

Posted at

はじめに

この記事は2022年月7にまとめていた「細かいつまずいたことをメモしておく(7月編)をそれぞれ投稿した内容になります
解決方法が最新でない可能性もありますのでご了承ください

問題

VSCode+WSL2+Docker+Goの環境でGOの拡張機能をいれたところ、起動時にVSCodeの左下に以下のエラーがでるようになりました

failed to find the "go" binary in either goroot()....

解決方法

これはローカルにGOをインストールすることで解決できました

またインストールしたところ以下のエラーに変わりました

the "gopls" command is not available. run "go install -v golang.org/x/tools/gopls@latest" to install.

WSL2で以下のコマンドを実行

$ sudo apt-get update
$ sudo apt install build-essential

そのあとエラーにあるAll installボタンをクリックすると直りました

注意

コンテナの中でGoを実行する場合はDockerfileにGoの拡張に必要なライブラリをいれて、そのあとリモートコンテナで開発する必要があります

ローカルのGo拡張を利用するとローカルのGOPATHのライブラリを参照するため、コンテナの中にはあるけどVSCodeの拡張的にはライブラリがないので赤線でエラーを表示してしまうからです

以下をDockerfileに加えてリモートコンテナでGoコンテナに入り拡張機能をインストールすることで利用できました

Dockerfile
# for Vscode extension
RUN go install github.com/ramya-rao-a/go-outline@latest
RUN go install golang.org/x/tools/gopls@latest
2
1
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
2
1