Skip to content

Instantly share code, notes, and snippets.

@AnuchitO
Created September 25, 2019 12:07
Show Gist options
  • Select an option

  • Save AnuchitO/97b30acc7bbdf82a6c582667527dff8f to your computer and use it in GitHub Desktop.

Select an option

Save AnuchitO/97b30acc7bbdf82a6c582667527dff8f to your computer and use it in GitHub Desktop.
func readFile() error {
return &os.PathError{Op: "read", Path: "/a/b", Err: errors.New("read error")}
}
func main() {
err := readFile()
// การ assert type แบบเดิม
if e, ok := err.(*os.PathError); ok {
fmt.Printf("type assertion, error match os.PathError: %#v\n", e)
}
// เปลี่ยนมาใช้ฟังก์ชั่น As
var e *os.PathError
if errors.As(err, &e) {
fmt.Printf("type assertion, error match os.PathError: %#v\n", e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment