当前位置: 首页 > news >正文

vscode使用delve调试golang程序

环境配置

delve仓库,含有教程:https://github.com/go-delve/delve

golang的debugging教程:https://github.com/golang/vscode-go/wiki/debugging

> go version
go version go1.20 windows/amd64> go install github.com/go-delve/delve/cmd/dlv@latest

快捷键

F8 下一步F7 进入函数
Shift+F8 退出函数F9 下一个断点

golang的调试有三种模式

1、Launch package

// Launch package: Debug/test the package of the open file 
{"name": "Launch Package","type": "go","request": "launch","mode": "auto","args": ["new", " conda"],"program": "${fileDirname}"
}

从说明上看出这是调试当前打开的文件所在的包,所以当前打开的文件需要是main包,里面有main函数,或者是测试文件,也就是需要可直接运行的文件。类比于我们直接进入该目录执行go run

可以带上运行参数 args

测试程序

ctl/ctl.go
package mainimport ("fmt""voteapi/ctl/cmd"
)func main() {fmt.Println("xxx")cmd.Execute()
}

fmt.Println("xxx")打上断点,因为调试器会直接跳到第一个断点的地方,如果一个断点都没有那就直接结束了。

控制台输出

Starting: D:\dev\php\magook\trunk\server\golang\path\bin\dlv.exe dap --listen=127.0.0.1:20155 from D:\dev\php\magook\trunk\server\voteapi\ctl
DAP server listening at: 127.0.0.1:20155
Type 'dlv help' for list of commands.

mode参数的值

  • debug: build and debug a main package
  • test: build and debug a test
  • exec: debug a precompiled binary
    • The binary must be built with go build -gcflags=all="-N -l" to disable inlining and optimizations that can interfere with debugging.
  • auto: automatically choose between debug and test depending on the open file

2、Attach to process

// Attach to local process: Attach to an existing process by process ID
{"name": "Attach to Process","type": "go","request": "attach","mode": "local","processId": 0
}

attach: You can use this configuration to attach to a running process or a running debug session.

mode参数的值

  • local: attaches to a local process.
    • The binary must be built with go build -gcflags=all="-N -l" to disable inlining and optimizations that can interfere with debugging.
  • remote: attaches to an in-progress debug session run by an external server.

You can debug an already running program using the local mode type configuration. The Go extension will start dlv dap and configure it to attach to the specified process. Users can select the process to debug with one of the following options:

  • Specifying the numeric process id (PID) with the processId attribute.
  • Specifying the target program name in the processId attribute. If there are multiple processes matching the specified program name, the extension will show the list of matching processes at the start of the debug session.
  • Specifying 0 in the processId attribute and selecting the process from the drop-down menu at the start of the debug session.

attach 的 local 模式的应用场景是常驻内存的程序,比如 http server,我们编译运行程序后,来到VScode,主要配置 processId 属性,可以是进程ID,可以是进程名称,也可以给0,然后编辑器会弹出下拉框让你选择要attach上的程序。

示例,还是 ctl.go 文件,改一下

package mainimport ("fmt""net/http"
)func main() {http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "%s", "hello")})http.ListenAndServe(":9999", nil)
}
go build ctl.go
ctl.exe

我们选择Attach to Process,点击运行调试三角形按钮,在弹出的下拉框输入ctl.exe,启动调试,同样的要先打几个断点。

在这里插入图片描述

此时调试工具栏是这样的

在这里插入图片描述

是出于暂停状态的,我们在浏览器请求一下这个服务,它就被激活了,就可以逐步调试了。

在这里插入图片描述

一个请求被调试完之后,调试不会结束,而是暂停了,等待下一个请求来触发。

3、Connect to server

说明 https://github.com/golang/vscode-go/wiki/debugging#remote-debugging

这是 vscode 的 Remote debugging 功能,他是基于 Remote Development,即远程开发,远程调试,这还需要安装额外的vscode扩展,将远程代码映射到本地,然后本地代码能时时推送到远程服务器,最终在远程服务器上运行代码。

我是直接在本地来的,发现也可以调试。

package mainimport ("fmt""voteapi/ctl/cmd"
)func main() {fmt.Println("xxx")cmd.Execute()
}
// Connect to server: Connect to a remote headless debug server
{"name": "Connect to server","type": "go","debugAdapter": "dlv-dap","request": "attach","mode": "remote","remotePath": "${workspaceFolder}","port": 2345,"host": "127.0.0.1"
}
dlv debug --headless --listen=:2345

launch.json配置项

文档 https://github.com/golang/vscode-go/wiki/debugging#configuration

// 命令行参数,要自己打空格
"args": ["-config", " server.json"],// 编译参数
"buildFlags": "-tags 'server'",// key:value
"env": {},
http://www.lryc.cn/news/160941.html

相关文章:

  • 如何从任何苹果、Windows或安卓设备访问iCloud照片
  • 关于“找不到mfc140u.dll,无法继续执行代码”问题的分析处理方法
  • 用 TripletLoss 优化bert ranking
  • Tomcat安装及使用
  • 法国新法案强迫 Firefox 等浏览器审查网站
  • 开源电商项目 Mall:构建高效电商系统的终极选择
  • QT(9.1)对话框与事件处理
  • C++项目实战——基于多设计模式下的同步异步日志系统-③-前置知识补充-设计模式
  • C++ 新旧版本两种读写锁
  • ES6 字符串的repeat()方法
  • 【车载以太网测试从入门到精通】系列文章目录汇总
  • LLM推理优化技术综述:KVCache、PageAttention、FlashAttention、MQA、GQA
  • go开发之个微机器人的二次开发
  • 2023国赛数学建模B题思路代码 - 多波束测线问题
  • SpringAOP面向切面编程
  • A Guide to Java HashMap
  • LeetCode 449. Serialize and Deserialize BST【树,BFS,DFS,栈】困难
  • 嵌入式IDE(1):IAR中ICF链接文件详解和实例分析
  • 分布式版本控制工具——git
  • C基础-数组
  • springboot项目配置flyway菜鸟级别教程
  • 成都精灵云初试
  • css relative 和absolute布局
  • 更健康舒适更科技的照明体验!书客SKY护眼台灯SUKER L1上手体验
  • 经管博士科研基础【19】齐次线性方程组
  • django报错解决 Forbidden (403) CSRF verification failed. Request aborted.
  • k8s-实战——yapi平台部署
  • Excel VSTO开发5 -Excel对象结构
  • Javafx集成sqlite数据库
  • react-native实现 TextInput 键盘显示搜索按钮并触发回调