一、参考
二、名词解释
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.
三、实际使用
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
文件
序号 | 描述 | 示例 |
---|---|---|
1 | lsp-workspace-folders-add | 一开始没有连接到 lsp-server , 可以通过命令M-x lsp-workspace-folders-add 创建连接, |
2 | 添加项目根目录 | |
3 | 建立连接 |
一般进入目录时候,会有提示,配置lsp-server
,此处选择 i
3.3 lsp-mode 中的常用命令
四、代码debug
4.1 开启debug
序号 | 命令 | 说明 | 示例 |
---|---|---|---|
1 | dap-debug | M-x dap-debug 开启项目debug | |
2 | 选择debug 模版 | 此处是对于单个函数的debug,选择 Go Dlv Test Current Function Configuration | |
3 | debug 详情页 |
4.2 集成命令面板 dap-hydra
通过命令M-x dap-hydra
可以进入debug命令终端
经常使用命令如下
序号 | 命令 | 说明 | 示例 |
---|---|---|---|
1 | ee | 用于查看debug过程中的变量信息 | |
2 | sl | 查看所有的变量 |
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。