1
2
3
4
5
6
7 package main
8
9 var out int
10 var zero int
11
12 func main() {
13 wantPanic("test1", func() {
14 out = 1 / zero
15 })
16 wantPanic("test2", func() {
17 _ = 1 / zero
18 })
19 wantPanic("test3", func() {
20 v := 0
21 _ = 1 / v
22 })
23 wantPanic("test4", func() { divby(0) })
24 }
25
26 func wantPanic(test string, fn func()) {
27 defer func() {
28 if e := recover(); e == nil {
29 panic(test + ": expected panic")
30 }
31 }()
32 fn()
33 }
34
35
36 func divby(v int) {
37 _ = 1 / v
38 }
39
View as plain text