Created
February 5, 2022 13:30
-
-
Save christian-korneck/eb1bcb226d8317db2464f12b6567f1bf to your computer and use it in GitHub Desktop.
golang errors.As()
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
package main | |
import ( | |
"errors" | |
"fmt" | |
"io/fs" | |
"os" | |
) | |
// output: Failed at path: non-existing | |
func main() { | |
if _, err := os.Open("non-existing"); err != nil { | |
var pathError *fs.PathError | |
if errors.As(err, &pathError) { | |
fmt.Println("Failed at path:", pathError.Path) | |
} else { | |
fmt.Println(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment