| 1 | package A1 |
|---|---|
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | . "fmt" |
| 6 | myfmt "fmt" |
| 7 | "os" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | func example(n int) { |
| 12 | x := "foo" + strings.Repeat("\t", n) |
| 13 | // Match, despite named import. |
| 14 | errors.New(x) |
| 15 | |
| 16 | // Match, despite dot import. |
| 17 | errors.New(x) |
| 18 | |
| 19 | // Match: multiple matches in same function are possible. |
| 20 | errors.New(x) |
| 21 | |
| 22 | // No match: wildcarded operand has the wrong type. |
| 23 | myfmt.Errorf("%s", 3) |
| 24 | |
| 25 | // No match: function operand doesn't match. |
| 26 | myfmt.Printf("%s", x) |
| 27 | |
| 28 | // No match again, dot import. |
| 29 | Printf("%s", x) |
| 30 | |
| 31 | // Match. |
| 32 | myfmt.Fprint(os.Stderr, errors.New(x+"foo")) |
| 33 | |
| 34 | // No match: though this literally matches the template, |
| 35 | // fmt doesn't resolve to a package here. |
| 36 | var fmt struct{ Errorf func(string, string) } |
| 37 | fmt.Errorf("%s", x) |
| 38 | |
| 39 | // Recursive matching: |
| 40 | |
| 41 | // Match: both matches are well-typed, so both succeed. |
| 42 | errors.New(errors.New(x + "foo").Error()) |
| 43 | |
| 44 | // Outer match succeeds, inner doesn't: 3 has wrong type. |
| 45 | errors.New(myfmt.Errorf("%s", 3).Error()) |
| 46 | |
| 47 | // Inner match succeeds, outer doesn't: the inner replacement |
| 48 | // has the wrong type (error not string). |
| 49 | myfmt.Errorf("%s", errors.New(x+"foo")) |
| 50 | } |
| 51 |
Members