vscode使用delve调试golang程序

2023-10-28

环境配置

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 main

import (
	"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 main

import (
	"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 main

import (
	"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": {},

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vscode使用delve调试golang程序 的相关文章

  • juc辅助类

    目录 减少计数 CountDownLatch 循环栅栏 CyclicBarrier 减少计数 CountDownLatch 循环栅栏 CyclicBarrier 两者区别 信号灯 Semaphore juc辅助类有 减少计数 CountDo

随机推荐

  • Doxygen文档生成工具

    Doxygen代码文档生成工具 文章目录 Doxygen代码文档生成工具 Doxygen Doxygen的注释 vscode插件 Doxygen实际使用 Doxygen 根据百度百科说法 Doxygen是一种开源的 跨平台的文档系统 支持C
  • 记一次由于外部K8S集群证书到期导致Jenkins无法生成动态agent节点错误解决(入坑出坑)...

    欢迎关注 WeiyiGeek 公众号 点击 下方卡片 即可关注我哟 设为 星标 每天带你 基础入门 到 进阶实践 再到 放弃学习 涉及 网络安全运维 应用开发 物联网IOT 学习路径 个人感悟 等知识 花开堪折直须折 莫待无花空折枝 作者主
  • Flask虚拟环境

    一 虚拟环境 1 为什么需要虚拟环境 虚拟环境作用是将每个项目所需要的包隔离开形成一个独立的整体 每个虚拟环境互不干涉 方便与运行 因为如果有包有升级的话 可能会运行不了 2 安装使用虚拟环境 pipenv 2 1 安装虚拟环境 在系统命行
  • uview2.0自定义u-count-down倒计时

    1 效果展示 2 思路需要后端返回一个结束的时间戳 注意是毫秒时间戳 如果是秒需要在后面加3个0转为毫秒 获取当当前时间戳 当前时间戳减去商品结束的时间戳得出要倒计时的时间戳 然后再进行值得处理 3 代码展示
  • 分享一个放烟花的特效

    先看效果 再看代码
  • 如何mock系统调用

    背景 Linux下开发存储系统 网络库的时候会用到一系列Linux的系统调用 每一个系统调用都有一些出错的场景 有些场景很极端 比如内存使用达到上限 磁盘写满等 如果对其进行测试的话 很难去构造这样的一个场景 这个时候集成测试就显得力不存心
  • React如何从后端获取数据并渲染到前端?

    React js 自己的定位是 A JavaScript Library for building user interface 它的文档称许多人将它用作 MVC 的 V 因此 React js 不关心你是如何嵌入后端数据的 换句话说 我们
  • css进阶(背景颜色渐变、过渡)

    css进阶 背景颜色径向渐变 背景颜色线性渐变 过渡 折叠效果 背景颜色径向渐变 径向渐变 中间部分椭圆形 四周填充 由中间到四周渐变 background radial gradient red yellow 红椭圆填充黄色 backgr
  • 在vue3中使用富文本编辑wangEditor上传自定义图片

    富文本编辑器wangEditor的安装和使用我就不在这里做介绍了 大家可以去官网进行查看 wangeditor官网 https www wangeditor com 接下来 进入主题 上传本地图片到后端 后端返回图片地址 在插入编辑器中 c
  • Install Google-Chrome

    首先将google的yum源下载下来 google并没有直接提供yum源 而是以sh文件的方式提供 那么就下载这个文件 使用以下地址来下载文件 https dl ssl google com linux google repo setup
  • leetcode----JavaScript 详情题解(1)

    目录 2618 检查是否是类的对象实例 2619 数组原型对象的最后一个元素 2620 计数器 2621 睡眠函数 2622 有时间限制的缓存 2623 记忆函数 2625 扁平化嵌套数组 2626 数组归约运算 2627 函数防抖 261
  • mdltxdy && mjj的英语单词(4.2洛谷比赛中的字符串问题)

    enmmmm我的字符串是真的凉啊orz 导致我到现在都怀疑那位负责出题的mjj是不是故意在卡我QAQ 好啦话不多说 上题 1 mdltxdy 字符串替换问题 题意描述 mdl每天都在小分队里面被刷屏 因此她急切的找到了你希望你写一个程序屏蔽
  • 利用gdb调试nginx

    利用gdb调试nginx 1 打开nginx调试 首先修改 auto cc conf文件 将ngx compile opt c 改为ngx compile opt c g 打开debug模式进行编译 简单操作如下 sudo configur
  • 图结构的基础实现(java)

    package graph import edu princeton cs algs4 Bag import edu princeton cs algs4 In public class Graph private final int V
  • NCL windows系统安装

    http www doc88 com p 192266283281 html NCL在Linux下的安装非常容易 只需下载适当版本的文件 设置好环境变量即可使用 NCL在Windows下的安装则要麻烦一些 需要先安装一个虚拟Linux环境
  • python3 [爬虫入门实战]爬虫之scrapy爬取织梦者网站并存mongoDB

    主要爬取了编程栏目里的其他编程里的36638条数据 过程是自己一步一步的往下写的 有不懂的也是一边找笔记 一边百度 一边调试 遗憾 没有进行多栏目数据的爬取 只爬了一个栏目的数据 希望有想法的有钻研精神的可以自己去尝试爬取一下 难度应该不会
  • [Process] kill() returned unexpected error 1

    Process kill returned unexpected error 1 系统CatAlina xcode版本11 3 iOS 13 3运行的时候会出现 Process kill returned unexpected error
  • 【计算机视觉

    文章目录 一 分割 语义相关 9篇 1 1 OpenIns3D Snap and Lookup for 3D Open vocabulary Instance Segmentation 1 2 dacl10k Benchmark for S
  • CRNN+CTC实现不定长验证码识别(keras模型-示例篇)

    目录 前言 运行环境 生成数据集 构建网络模型 训练模型 测试模型 错误集锦 结语 前言 本文的重心在于如何使用以tensorflow作为后端的keras构建一个使用CTC为loss的简化版CRNN 同时指出构建过程中容易出错的地方 让像我
  • vscode使用delve调试golang程序

    环境配置 delve仓库 含有教程 https github com go delve delve golang的debugging教程 https github com golang vscode go wiki debugging gt