头图

一、参考

emacs系列文章目录——更新ing

lsp-mode

lsp-mode: go config

dap-mode

dap-mode: go config

二、名词解释

2.1 gopls

go 语言服务器,实现lsp协议 的服务端

gopls (pronounced "Go please") is the official Go language server developed by the Go team. It provides IDE features to any LSP-compatible editor.

You should not need to interact with gopls directly--it will be automatically integrated into your editor. The specific features and settings vary slightly by editor, so we recommend that you proceed to the documentation for your editor below.

2.2 lsp-mode

lsp-mode emacs中的lsp协议 的客户端

Client for Language Server Protocol (v3.14). lsp-mode aims to provide IDE-like experience by providing optional integration with the most popular Emacs packages like company, flycheck and projectile.

2.3 delve

go语言的debugger, 当作实现 dap协议 的服务端

Delve is a debugger for the Go programming language. The goal of the project is to provide a simple, full featured debugging tool for Go. Delve should be easy to invoke and easy to use. Chances are if you're using a debugger, things aren't going your way. With that in mind, Delve should stay out of your way as much as possible.

2.4 dap-mode

实现 dap协议的客户端

Emacs client/library for Debug Adapter Protocol is a wire protocol for communication between client and Debug Server. It’s similar to the LSP but provides integration with debug server.

image.png

三、实际使用

3.1 go语言的 init.el 配置


(use-package go-mode
  :mode
  (("\\.go\\'" . go-mode))
  :config
  (setq exec-path (append exec-path '("/usr/local/go/bin"))) ;; 设置golang的编译路径
  (setq gofmt-command "goimports")
  (setq tab-width 2)
  (setq indent-tabs-mode 1)
  (require 'lsp-mode)
  (require 'dap-mode)
  (require 'dap-dlv-go)
  :hook
  ;; (add-hook 'before-save-hook 'gofmt-before-save)
  (before-save . gofmt-before-save)
  (before-save . lsp-format-buffer)
  (before-save . lsp-organize-imports)
  (go-mode . flycheck-mode)
  (go-mode . company-mode)
  (go-mode . hs-minor-mode)
  (go-mode . lsp-deferred)
  :bind
  (:map go-mode-map ;; 定义本地命名空间
    ("M-." . godef-jump)
    ("M-," . pop-tag-mark))
  )

3.2 开启项目的 lsp-mode

➜  yzgo pwd
/Users/yz/work/github/yzgo
➜  yzgo tree .
.
├── go.mod
└── string_test.go

0 directories, 2 files
➜  yzgo cat go.mod
module yzgo.com/m

go 1.20
➜  yzgo cat string_test.go
package main

import (
    "fmt"
    "testing"
    "time"
)

func TestS1(t *testing.T) {

    fmt.Println("---start--", time.Now())

    a := 1
    b := 2
    c := a + b
    fmt.Println("--end--", time.Now(), a, b, c)
}

由上可见,有一个简单的项目 yzgo,只有一个简单的string_test.go文件

序号描述示例
1lsp-workspace-folders-add一开始没有连接到 lsp-server, 可以通过命令M-x lsp-workspace-folders-add创建连接, image.png
2添加项目根目录image.png
3建立连接image.png

一般进入目录时候,会有提示,配置lsp-server,此处选择 i

image.png

3.3 lsp-mode 中的常用命令

四、代码debug

4.1 开启debug

序号命令说明示例
1dap-debugM-x dap-debug开启项目debugimage.png
2选择debug 模版此处是对于单个函数的debug,选择 Go Dlv Test Current Function Configurationimage.png
3debug 详情页 image.png

4.2 集成命令面板 dap-hydra

通过命令M-x dap-hydra可以进入debug命令终端

image.png

经常使用命令如下

序号命令说明示例
1ee用于查看debug过程中的变量信息image.png
2sl查看所有的变量image.png

一曲广陵散
76 声望21 粉丝

柴米油盐酱醋茶