参考答案
使用pprof和runtime接口定位goroutine溢出。核心要点:引入net/http/pprof包,访问/debug/pprof/goroutine查看栈和数量;代码中调用runtime.NumGoroutine()定期打印监控增长;利用go tool pprof分析阻塞点;常见原因为channel未关闭、无context取消导致goroutine永久阻塞。原理:goroutine泄漏使数量只增不减,pprof的goroutine profile列出所有存活协程调用栈。示例:import _ "net/http/pprof"; 然后go tool pprof http://localhost:8080/debug/pprof/goroutine。修复需确保退出路径关闭channel或取消context。