Created
September 25, 2019 12:07
-
-
Save AnuchitO/97b30acc7bbdf82a6c582667527dff8f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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