GoLand无法直接查看goroutine数量,需通过HTTP接口手动请求/debug/pprof/goroutine?debug=2或使用go tool pprof命令行分析,关键注意监听地址绑定和debug参数。

GoLand 里直接看不了 goroutine 数量,得靠 HTTP 接口 + 外部工具
GoLand 本身不解析或渲染 /debug/pprof/goroutine 的响应内容,它没有内置的 pprof 可视化面板。你看到的“Run with pprof”选项只支持 CPU / heap profile 的自动采集和图形化,对 goroutine profile 无效。想查数量,必须走 HTTP 路径手动请求或用 go tool pprof 命令行分析。
确保 pprof HTTP 接口在 GoLand 启动时真正暴露
常见错误是:代码里写了 _ "net/http/pprof",但服务启动后 curl http://localhost:6060/debug/pprof/ 返回 404 或 connection refused。
GoLand 2026.1.1 是 2026.1 发布后的首个维护修正版本,适合已经开始体验 2026.1 新功能并希望同步补丁的开发者。它更适合用于入门项目、现有项目迁移测试和 IDE 行为验证。
- 检查
http.ListenAndServe是否绑定了"0.0.0.0:6060"(不是":6060")——Go 1.19+ 默认只监听localhost,GoLand 远程调试或容器场景下会连不上 - 确认没在
main()之前 panic,或被log.Fatal提前退出,导致 pprof handler 没注册上 - 如果用了自定义
http.ServeMux,必须显式挂载:http.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index)),不能只依赖_ "net/http/pprof" - GoLand 的 Run Configuration 里勾选 “Allow unsigned requests” 没用,pprof 不走那套;重点是进程是否真在监听且可访问
用 curl 或 go tool pprof 快速提取协程总数
?debug=2 是关键开关,漏掉就只能看到 running 状态的少量 goroutine,无法反映真实总量。
- 取总数最轻量:
curl -s 'http://localhost:6060/debug/pprof/goroutine?debug=2' | head -n1→ 输出类似goroutine profile: total 42 - 要深度排查泄漏:保存两个快照
curl -s 'http://localhost:6060/debug/pprof/goroutine?debug=2' > g1.txt,等 30 秒再抓一次g2.txt,然后用diff g1.txt g2.txt | grep "created by"找新增来源 - 命令行交互分析:
go tool pprof http://localhost:6060/debug/pprof/goroutine?debug=2,进 CLI 后输top看高频创建点,list main.writeLog查具体函数调用链
别把 runtime.NumGoroutine() 和 pprof 混用成同一类监控
runtime.NumGoroutine() 返回整数,适合打点上报;/debug/pprof/goroutine?debug=2 返回结构化栈迹,用于诊断。两者目的不同,不能互相替代。
- 在健康检查 endpoint 里嵌
runtime.NumGoroutine()没问题,但数值突增时你不知道卡在哪——这时候必须切到 pprof 抓快照 -
GODEBUG=gctrace=1之类调试变量会让runtime.NumGoroutine()和 pprof 统计出现偏差,排查阶段建议关闭 - 生产环境开启 pprof 接口必须加网络层限制(如 Nginx IP 白名单、K8s NetworkPolicy),
?debug=2响应体虽小,但暴露全部 goroutine 创建位置,属于敏感信息

















