LoginSignup
1
0

More than 3 years have passed since last update.

Spacemacs + go layer + LSP + DAP + Delve で快適Go!

Posted at

EmacsでGo書いてデバッグする

かなり前からEmacs + (C/C++) + gdb = IDEとしても使えるEmacsですのでGo言語でも同じようにできます。

Emacs

お好きなディストリビューションをお使い下さい。

このエントリーではSpacemacsを前提に進めます。

Go言語

go layerを使います。

Language Server Protocol (LSP)

もちろんLSPも有効にしましょう。

lsp layerを使います。

lsp-modeがEmacs用のクライアントです。

Debug Adapter Protocol (DAP)

今回の目的達成にはこの機能をSpacemacsに追加します。

lsp-modeも勿論DAPをサポートしています。

対応言語が増えていますので一度確認されることをおすすめします。

Delve

Go言語用のデバッガです。

C/C++でいうgdbに相当します。 (CLIも同じ!!)

init.el

Layerの設定例です。

(setq-default dotspacemacs-configuration-layers
              '(
                (lsp :variables
                     lsp-navigation 'peek)
                (go :variables
                    go-backend 'lsp
                    )
                dap
                ))

さっそくEmacs IDEでデバッグ

の前にもう少し設定が必要です。

dap + delve がどのようにデバッグ対象を起動するかを登録します。

(dap-register-debug-template "dap sample config"
                             (list :type "go"
                                   :request "launch"
                                   :name "sample launch"
                                   :mode "debug"
                                   :program: "~/src/github.com/dap-sample"
                                   :buildFlags "-tags sample_tag"
                                   :args '()
                                   :env '() ; '(("DEV" . "1"))
                                   :envFile ".envrc"
                                   )
                             )

evalするとこうなります。

| dap sample config                                 | :type | go | :request | launch | :name | sample launch                    | :mode | debug  | :program: | ~/src/github.com/dap-sample | :buildFlags | -tags sample_tag | :args | nil | :env     | nil | :envFile | .envrc |
| Go Connect Remote dlv Configuration               | :type | go | :request | launch | :name | Connect to Remote dlv            | :mode | remote | :program  | nil                         | :args       | nil              | :env  | nil | :envFile | nil |          |        |
| Go Attach Executable Configuration                | :type | go | :request | attach | :name | Attach to Executable             | :mode | local  | :program  | nil                         | :args       | nil              | :env  | nil | :envFile | nil |          |        |
| Go Launch Executable Configuration                | :type | go | :request | launch | :name | Launch Executable                | :mode | exec   | :program  | nil                         | :args       | nil              | :env  | nil | :envFile | nil |          |        |
| Go Launch Unoptimized Debug Package Configuration | :type | go | :request | launch | :name | Launch Unoptimized Debug Package | :mode | debug  | :program  | nil                         | :buildFlags | -gcflags '-N -l' | :args | nil | :env     | nil | :envFile | nil    |
| Go Launch Debug Package Configuration             | :type | go | :request | launch | :name | Launch Debug Package             | :mode | debug  | :program  | nil                         | :buildFlags | nil              | :args | nil | :env     | nil | :envFile | nil    |
| Go Launch File Configuration                      | :type | go | :request | launch | :name | Launch File                      | :mode | auto   | :program  | nil                         | :buildFlags | nil              | :args | nil | :env     | nil | :envFile | nil    |

"dap sample config" 以外はデフォルト用意されているものです。

登録したらSPC d d d:(dap-debug)で起動します。

登録した内容の修正はSPC d d e:(dap-debug-edit-template)です。

修正したら忘れずに(eval-buffer)してください。

参考:

launch.json

(dap-register-debug-template)の内容はVS CodeでGo + Delveを使う際に参照されるlaunch.jsonと同じ内容になります。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "MyGet",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "/home/h3poteto/src/github.com/h3poteto/package-name",
            "env": {},
            "args": "get"
        }
    ]
}

参考:

さいごに

中々うまくいかなかったこの組み合わせのデバッグですが、できるようになると作業が捗りますので是非トライしてみてください。

Let's enjoy ! Happy Hacking !

参考リンク

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