| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | ) |
| 8 | |
| 9 | func main() { |
| 10 | assertInt() |
| 11 | assertNil() |
| 12 | assertValue() |
| 13 | } |
| 14 | |
| 15 | func assertInt() { |
| 16 | defer func() { |
| 17 | r := recover() |
| 18 | fmt.Println(r) |
| 19 | }() |
| 20 | |
| 21 | var v interface{} = 1 |
| 22 | println(v.(string)) |
| 23 | } |
| 24 | |
| 25 | func assertNil() { |
| 26 | defer func() { |
| 27 | r := recover() |
| 28 | fmt.Println(r) |
| 29 | }() |
| 30 | |
| 31 | var v interface{} |
| 32 | println(v.(string)) |
| 33 | } |
| 34 | |
| 35 | func assertValue() { |
| 36 | defer func() { |
| 37 | r := recover() |
| 38 | fmt.Println(r) |
| 39 | }() |
| 40 | |
| 41 | var v http.ResponseWriter = httptest.NewRecorder() |
| 42 | println(v.(http.Pusher)) |
| 43 | } |
| 44 | |
| 45 | // Output: |
| 46 | // 22:10: interface conversion: interface {} is int, not string |
| 47 | // 32:10: interface conversion: interface {} is nil, not string |
| 48 | // 42:10: interface conversion: *httptest.ResponseRecorder is not http.Pusher: missing method Push |