IT评测·应用市场-qidao123.com技术社区
标题:
在Goland中对goroutine协程断点调试
[打印本页]
作者:
拉不拉稀肚拉稀
时间:
2024-12-10 10:50
标题:
在Goland中对goroutine协程断点调试
在Goland中对goroutine协程断点调试
环境: Goland
参考了 chatgpt 的回复
进行断点调试的代码
package main
import (
"fmt"
"sync"
"time"
)
// worker 模拟处理任务
func worker(id int, wg *sync.WaitGroup) {
defer wg.Done() // 确保任务完成后通知 WaitGroup
for i := 0; i < 5; i++ {
fmt.Printf("Worker %d: Processing step %d\n", id, i)
time.Sleep(500 * time.Millisecond) // 模拟耗时任务
}
fmt.Printf("Worker %d: Task completed\n", id)
}
func main() {
var wg sync.WaitGroup
// 启动 3 个 goroutine
for i := 1; i <= 3; i++ {
wg.Add(1)
go worker(i, &wg)
}
fmt.Println("All workers started. Waiting for completion...")
wg.Wait() // 等待所有任务完成
fmt.Println("All workers completed.")
}
复制代码
在 worker 函数内 fmt.Printf("Worker %d: Processing step %d\n", id, i) 这一行设置断点
在 Goland 配置调试
当程序在断点暂停时,打开 Goroutines 面板,检察当前所有运行的 goroutine
利用调试工具中的 Step Over (F8) 或 Step Into (F7),逐步分析协程的行为
在 Variables 面板中检察每个协程的局部变量(如 id 和 i 的值)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/)
Powered by Discuz! X3.4