Skip to content

Instantly share code, notes, and snippets.

@infoverload
Created November 20, 2019 13:45
Show Gist options
  • Save infoverload/c82a734f16f31e317cf1bf3b52c13844 to your computer and use it in GitHub Desktop.
Save infoverload/c82a734f16f31e317cf1bf3b52c13844 to your computer and use it in GitHub Desktop.
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