1
2
3
4
5
6
7 package main
8
9 type I interface{}
10
11 func foo1(i int) int { return i }
12 func foo2(i int32) int32 { return i }
13 func main() {
14 var i I
15 i = 1
16 var v1 = i.(int)
17 if foo1(v1) != 1 {
18 panic(1)
19 }
20 var v2 = int32(i.(int))
21 if foo2(v2) != 1 {
22 panic(2)
23 }
24
25 shouldPanic(p1)
26 }
27
28 func p1() {
29 var i I
30 i = 1
31 var v3 = i.(int32)
32 if foo2(v3) != 1 {
33 panic(3)
34 }
35 }
36
37 func shouldPanic(f func()) {
38 defer func() {
39 if recover() == nil {
40 panic("function should panic")
41 }
42 }()
43 f()
44 }
45
View as plain text