1
2
3
4
5
6
7 package main
8
9 import (
10 "fmt"
11 "runtime"
12 "strings"
13 )
14
15 type T struct {
16 val int
17 }
18
19 func main() {
20 defer expectError(22)
21 var pT *T
22 switch pT.val {
23 case 0:
24 fmt.Println("0")
25 case 1:
26 fmt.Println("1")
27 case 2:
28 fmt.Println("2")
29 }
30 fmt.Println("finished")
31 }
32
33 func expectError(expectLine int) {
34 if recover() == nil {
35 panic("did not crash")
36 }
37 for i := 1;; i++ {
38 _, file, line, ok := runtime.Caller(i)
39 if !ok {
40 panic("cannot find issue4562.go on stack")
41 }
42 if strings.HasSuffix(file, "issue4562.go") {
43 if line != expectLine {
44 panic(fmt.Sprintf("crashed at line %d, wanted line %d", line, expectLine))
45 }
46 break
47 }
48 }
49 }
50
View as plain text