<\/span><\/h1>\n\n\n\n Let’s see an example<\/p>\n\n\n\n
package main\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n)\n\nfunc main() {\n\n\terr := openFile(\"non-existing.txt\")\n\n\tif e, ok := err.(*os.PathError); ok {\n\t\tfmt.Printf(\"Using Assert: Error e is of type path error. Path: %v\\n\", e.Path)\n\t} else {\n\t\tfmt.Println(\"Using Assert: Error not of type path error\")\n\t}\n\n\tvar pathError *os.PathError\n\tif errors.As(err, &pathError) {\n\t\tfmt.Printf(\"Using As function: Error e is of type path error. Path: %v\\n\", pathError.Path)\n\t}\n}\n\nfunc openFile(fileName string) error {\n\t_, err := os.Open(\"non-existing.txt\")\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nUsing Assert: Error e is of type path error. Path: non-existing.txt\nUsing As function: Error e is of type path error. Path: non-existing.txt<\/code><\/pre>\n\n\n\nIn the above program, we have a function openFile in which we are trying to open a non-existing type hence it will raise an error. Then we are asserting the error in two ways<\/p>\n\n\n\n
Using .assert operator. The ok variable will be set to true if the error underlying type is *os.PathError<\/strong> otherwise it will be set to false<\/li><\/ul>\n\n\n\ne,ok := err.(*os.PathError); ok<\/code><\/pre>\n\n\n\nUsing the As<\/strong> function of errors package<\/li><\/ul>\n\n\n\nerrors.As(err, &pathError)<\/code><\/pre>\n\n\n\nBoth the method correctly assert that the error is of type *os.PathError<\/strong> as error returned by the openFile<\/strong> function is of type *os.PathError<\/strong><\/p>\n\n\n\nWe mentioned above that using As<\/strong> the function is preferable to using the .({type}) assert because it checks for a match by unwrapping the first error sequentially and matches it with the target error at each step of unwrap. Let’s see an example to understand that<\/p>\n\n\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n)\n\nfunc main() {\n\tvar pathError *os.PathError\n\terr := openFile(\"non-existing.txt\")\n\n\tif e, ok := err.(*os.PathError); ok {\n\t\tfmt.Printf(\"Using Assert: Error e is of type path error. Error: %v\\n\", e)\n\t} else {\n\t\tfmt.Println(\"Using Assert: Error not of type path error\")\n\t}\n\n\tif errors.As(err, &pathError) {\n\t\tfmt.Printf(\"Using As function: Error e is of type path error. Error: %v\\n\", pathError)\n\t}\n}\n\nfunc openFile(fileName string) error {\n\t_, err := os.Open(\"non-existing.txt\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Error opening: %w\", err)\n\t}\n\treturn nil\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nUsing Assert: Error not of type path error\nUsing As function: Error e is of type path error. Error: open non-existing.txt: no such file or directory<\/code><\/pre>\n\n\n\nThe above program is almost same as the previous program just the difference being that in the openFile function we are wrapping the error as well<\/p>\n\n\n\n
return fmt.Errorf(\"Error opening: %w\", err)<\/code><\/pre>\n\n\n\nThe . assert outputs<\/li><\/ul>\n\n\n\nUsing Assert: Error not of type path error<\/code><\/pre>\n\n\n\nWhile As function outputs<\/li><\/ul>\n\n\n\nUsing As function: Error e is of type path error. Error: open non-existing.txt: no such file or directory<\/code><\/pre>\n\n\n\nThis is because the error returned by the openFile <\/strong>function wraps *os.Patherror<\/strong> error which is not catched by the dot(‘.’) assert but is catched by As<\/strong> function<\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewCode Overview There are two ways of getting the underlying type Using the .({type}) assert If the assert succeeds then it will return the corresponding error otherwise it will…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-3923","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Get underlying type from error or error assertion in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n