Error handling methods in golang

Release: 2019-12-07 09:55:00
Original
2982 people have browsed it

Error handling methods in golang

Error handling is very important. In the Go language, error handling is designed to be very simple.

If done well, it will be very helpful in troubleshooting problems; if done poorly, it will be more troublesome.

Starting from 1.0, errors are defined in go as error interface

// The error built-in interface type is the conventional interface for // representing an error condition, with the nil value representing no error. type error interface { Error() string }
Copy after login

In go language, there are several ways to handle errors:

1. The judgment values are equal. Like io.EOF, in go language, it is called sentinel error

2. Through assertion (type assertion or type switch), determine the type of err or whether a certain interface is implemented

3. Make use of the methods provided by the package. Like os.IsNotExist. In the go language, it is called ad-hoc check

4. When the above 3 methods are not available, search whether err.Error() contains a specific string. (Not recommended)

Try not to use sentinel error

Try not to use ad-hoc check

The above is the detailed content of Error handling methods in golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!