附录 D:让 Go panic 的十种方法

本章节有且仅有一个目的,那就是让你的 Go 程序遇到 panic。

D.1 数组/切片索引越界

  1. func main() {
  2. names := []string{
  3. "煎鱼",
  4. "eddycjy",
  5. "Go 语言编程之旅",
  6. }
  7. name := names[len(names)]
  8. fmt.Printf("name: %s", name)
  9. }

运行结果:

  1. panic: runtime error: index out of range [3] with length 3
  2. goroutine 1 [running]:
  3. main.main()
  4. /Users/eddycjy/go-application/awesomePanic/main.go:11 +0x1b

D.2 空指针调用

  1. type User struct {
  2. Name string
  3. }
  4. func (u *User) GetName() string {
  5. return u.Name
  6. }
  7. func main() {
  8. s := &User{Name: "煎鱼"}
  9. s = nil
  10. s.GetName()
  11. }

运行结果:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1056f62]
  3. goroutine 1 [running]:
  4. main.(*User).GetName(...)
  5. /Users/eddycjy/go-application/awesomePanic/main.go:8
  6. main.main()
  7. /Users/eddycjy/go-application/awesomePanic/main.go:14 +0x2

D.3 过早关闭 HTTP 响应体

  1. func main() {
  2. resp, err := http.Get("https://xxx")
  3. defer resp.Body.Close()
  4. if err != nil {
  5. log.Fatalf("http.Get err: %v", err)
  6. }
  7. }

运行结果:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x123d4d3]
  3. goroutine 1 [running]:
  4. main.main()
  5. /Users/eddycjy/go-application/awesomePanic/main.go:10 +0x63

D.4 除以零

  1. func divide(a, b int) int {
  2. return a / b
  3. }
  4. func main() {
  5. divide(1, 0)
  6. }

运行结果:

  1. panic: runtime error: integer divide by zero
  2. goroutine 1 [running]:
  3. main.divide(...)
  4. /Users/eddycjy/go-application/awesomePanic/main.go:4
  5. main.main()
  6. /Users/eddycjy/go-application/awesomePanic/main.go:8 +0x12

D.5 向已关闭的通道发消息

  1. func main() {
  2. ch := make(chan string, 1)
  3. ch <- "煎鱼"
  4. close(ch)
  5. ch <- "Go 语言编程之旅"
  6. }

运行结果:

  1. panic: send on closed channel
  2. goroutine 1 [running]:
  3. main.main()
  4. /Users/eddycjy/go-application/awesomePanic/main.go:7 +0x7d

D.6 重复关闭通道

  1. func main() {
  2. ch := make(chan string, 1)
  3. ch <- "Go 语言编程之旅"
  4. close(ch)
  5. close(ch)
  6. }

运行结果:

  1. panic: close of closed channel
  2. goroutine 1 [running]:
  3. main.main()
  4. /Users/eddycjy/go-application/awesomePanic/main.go:7 +0x71

D.7 关闭未初始化的通道

  1. func main() {
  2. var ch chan string
  3. close(ch)
  4. }

运行结果:

  1. panic: close of nil channel
  2. goroutine 1 [running]:
  3. main.main()
  4. /Users/eddycjy/go-application/awesomePanic/main.go:5 +0x2a

D.8 未初始化 map

  1. func main() {
  2. var m map[string]string
  3. m["Go 语言编程之旅"] = "一起用 Go 做项目"
  4. }

运行结果:

  1. panic: assignment to entry in nil map
  2. goroutine 1 [running]:
  3. main.main()
  4. /Users/eddycjy/go-application/awesomePanic/main.go:5 +0x4b

D.9 跨协程的恐慌处理

  1. func main() {
  2. go func() {
  3. defer func() {
  4. if err := recover(); err != nil {
  5. log.Fatalf("recover err: %v", err)
  6. }
  7. }()
  8. handle()
  9. }()
  10. time.Sleep(time.Second)
  11. }
  12. func handle() {
  13. go divide(1, 0)
  14. }
  15. func divide(a, b int) int {
  16. return a / b
  17. }

运行结果:

  1. panic: runtime error: integer divide by zero
  2. goroutine 17 [running]:
  3. main.divide(0x1, 0x0, 0x0)
  4. /Users/eddycjy/go-application/awesomePanic/main.go:34 +0x40
  5. created by main.handle
  6. /Users/eddycjy/go-application/awesomePanic/main.go:30 +0x47

D.10 sync 计数为负值

  1. func main() {
  2. wg := sync.WaitGroup{}
  3. wg.Add(-1)
  4. wg.Wait()
  5. }

运行结果:

  1. panic: sync: negative WaitGroup counter
  2. goroutine 1 [running]:
  3. sync.(*WaitGroup).Add(0xc000104000, 0xffffffffffffffff)
  4. /usr/local/Cellar/go/1.14/libexec/src/sync/waitgroup.go:74 +0x139
  5. main.main()
  6. /Users/eddycjy/go-application/awesomePanic/main.go:7 +0x44

本图书由 煎鱼©2020 版权所有,所有文章采用知识署名-非商业性使用-禁止演绎 4.0 国际进行许可。

附录 D:让 Go panic 的十种方法 - 图1