Created
November 20, 2019 13:45
-
-
Save infoverload/c82a734f16f31e317cf1bf3b52c13844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
) | |
type argError struct { | |
arg int | |
prob string | |
} | |
func (e *argError) Error() string { | |
return fmt.Sprintf("%d - %s", e.arg, e.prob) | |
} | |
func testfunction(arg int) (int, error) { | |
if arg == 42 { | |
return -1, &argError{arg, "can't work with it"} | |
} | |
return arg, nil | |
} | |
func main() { | |
_, e := testfunction(42) | |
if ae, ok := e.(*argError); ok { | |
fmt.Println(ae.arg) | |
fmt.Println(ae.prob) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment