| 类型不一致 |
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]=1 |
panic: assignment to entry in nil map |
| channel 关闭写入 |
ch := make(chan int); close(ch); ch<-1 |
panic: 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! |