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

golang 非error错误分类

1.应用级别,可recover

这些 panic 一般是 逻辑或使用不当导致的运行时错误,Go 程序可以用 recover 捕获并继续运行:

类型示例描述
类型不一致atomic.Value 存不同类型 v.Store(100); v.Store("abc")panic: store of inconsistently typed value
数组/切片越界arr := []int{1,2}; fmt.Println(arr[3])panic: runtime error: index out of range
空指针引用var p *int; fmt.Println(*p)panic: runtime error: invalid memory address or nil pointer dereference
类型断言失败var i interface{} = 123; s := i.(string)panic: interface conversion: int is not string
映射访问不存在键不会 panic 直接返回零值;但对 nil map 写入会 panic
nil map 写入var m map[int]int; m[1]=1panic: assignment to entry in nil map
channel 关闭写入ch := make(chan int); close(ch); ch<-1panic: send on closed channel
channel 关闭重复ch := make(chan int); close(ch); close(ch)panic: close of closed channel
阻塞,不 panic(可选)
runtime 错误runtime.Gosched() 不会 panic,但 runtime.Goexit() 会终止当前 goroutine
死锁进程内所有协程都在阻塞fatal error: all goroutines are asleep - deadlock!

2. 系统级别错误,不可recover

类型示例描述
map 并发读写m := make(map[int]int); go m[1]=1; go _ = m[2]panic: concurrent map read and map write
栈溢出无限递归函数panic: runtime: stack overflow
内存损坏Cgo 直接写越界内存runtime panic,无法 recover
runtime 系统错误runtime.Breakpoint()调试或系统级 panic,不可 recover
内存访问非法unsafe 包错误操作如访问非法地址、强制类型转换出界
signal / SIGSEGV非法访问地址panic: runtime error: invalid memory address or nil pointer dereference,在系统级别也可能终止进程

3. 特殊场景

类型示例描述
nil 函数调用var f func(); f()panic: call of nil function
append 到 nil slicevar s []int; s = append(s, 1)不 panic,可以正常工作
close nil channelvar ch chan int; close(ch)panic: close of nil channel
atomic.Value 读写不一致类型v.Store(100); v.Store("abc")panic,可 recover
http://www.dtcms.com/a/344742.html

相关文章:

  • 【如何生成专业级 API 接口文档:从规范到实战】
  • 指针实现数组的逆序存放并输出
  • IKE 与 ISAKMP 核心笔记
  • JCTools Spmc 单生产者-多消费者的无锁并发有界队列
  • 支持轻量化部署的混元3D世界模型Lite版本上线魔乐社区,昇腾部署实践来啦
  • FCT/ATE/ICT通用测试上位机软件
  • Leetcode—595. 大的国家【简单】
  • JUC之Fork/Join
  • WindowsAPI|每天了解几个winAPI接口之网络配置相关文档Iphlpapi.h详细分析9
  • 2-3.Python 编码基础 - 类型检测与类型转换
  • Vue 实现可拖拽分割布局(支持左右、上下拖拽调整)
  • Java 学习笔记(基础篇7)
  • 2025年游戏盾SDK动态加密技术全景解析:从防御破解到重塑游戏安全基石
  • CSM5110 5V/1A降压芯片 SOT23-5封装 可替代RY3408 带OVP保护
  • vim的使用
  • 牛客面经1 滴滴社招-002
  • JAVA国际版多商户运营版商城系统源码多商户社交电商系统源码支持Android+IOS+H5
  • 哈希和字符串哈希
  • STM32 外设驱动模块七:红外反射式光电模块
  • Centos 8 管理防火墙
  • 安装Tailscale
  • Maven初识到应用
  • 【AI应用】向量数据库Milvus详细命令
  • Jenkins + SonarQube 从原理到实战四:Jenkins 与 Gerrit 集成并实现自动任务
  • Linux爆音问题解决方法(隔一会会有奇怪噪音)
  • Go 基础解析
  • 逛越南本地菜市场学英语
  • 异质结3.0时代的降本提效革命:捷造科技设备技术创新与产业拐点分析
  • DSPy框架:从提示工程到声明式编程的革命性转变
  • go 常见面试题