1
3

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.

WSL2+GO+VSCODEで開発

Last updated at Posted at 2020-06-06

前置き

事前にWSL2のインストールとその環境下でDocker-composeが動くようにしてください。

フォルダ構成

テストするうえで、以下のフォルダ構成を行います。

./
├── Dockerfile
├── docker-compose.yml
└── golang
       └── main.go

各ファイルの中身について

Dockerfile
FROM golang
RUN go get github.com/derekparker/delve/cmd/dlv

WORKDIR /go/src/app
docker-compose.yml
version: '3'
services: 
  app:
    build: .
    ports:
    - "5050:5050" #delve用のポート
    volumes:
    - "./golang:/go/src/app"
    privileged: true
    command: dlv debug --headless --listen=:5050 --log --api-version=2
main.go
package main
import "fmt"

func main() {
	i := 0
	fmt.Println("hello world")
	fmt.Println("hello world")
	fmt.Println("hello world", i)
}

VScode設定

launch.jsonファイルを以下のように修正します。

launch.json
{
    // IntelliSense を使用して利用可能な属性を学べます。
    // 既存の属性の説明をホバーして表示します。
    // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [   
        {
            "name": "Launch",       //ローカル使用
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {},
            "args": []
        },
         {
            "name": "Remote",       //リモート使用
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "remotePath": "/go/src/app", //コンテナ内のパス
            "port": 5050, // dlv debugのポートを指定変更可能
            "host": "karosu-main", //wslを立ち上げたときに表示されるhost名を指定
            "program": "${fileDirname}",
            "env": {},
            "args": [],
            "showLog": true
        }
    ]
}

今回、wsl2のホスト名がkarosu-mainのためそのようにしていますが、人によって違うかもしれませんので、
実際にWslを起動して、hostnameコマンドで調べてみてください。
あと通信が確立しているかの確認はコマンドプロントpowershellを起動して、ping karosu-mainが通るか確認してください。

作業実行

以下の作業手順に沿って作業を行えば、確認できます。

  1. wslを起動
  2. docker-composeを起動
  3. 実行の構成でRemoteを選択
  4. main.goでブレイクポイント指定
  5. デバッグの開始

デバックのPrint文の出力は、ターミナル上に出力されます。

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?