| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
) |
| 7 |
|
| 8 |
type CustomError struct { |
| 9 |
Err string |
| 10 |
} |
| 11 |
|
| 12 |
func (ce CustomError) Error() string { |
| 13 |
return ce.Err |
| 14 |
} |
| 15 |
|
| 16 |
func SomeFunc() (string, *CustomError) { |
| 17 |
return "hello", nil |
| 18 |
} |
| 19 |
|
| 20 |
func main() { |
| 21 |
// Do something that creates a variable err of type error |
| 22 |
_, err := os.Open("/") |
| 23 |
if err != nil { |
| 24 |
panic(err) |
| 25 |
} |
| 26 |
|
| 27 |
// Then replace the err type with *CustomError |
| 28 |
val, err := SomeFunc() |
| 29 |
if err != nil { //@ diag(`this comparison is always true`) |
| 30 |
panic(err) |
| 31 |
} |
| 32 |
|
| 33 |
fmt.Println("No problem", val) |
| 34 |
} |