Error_PHP tutorial on golang error handling

Source: Internet
Author: User
Golang error handling error. In golang error handling, errorgolang does not have an exception handling mechanism such as trycatch. it can only rely on the returned value to determine whether the status is incorrect (of course, it has a panicrecover mechanism, but generally only golang error handling
Golang does not have an exception handling mechanism such as try/catch. it can only rely on the return value to determine whether the status is wrong (of course, it has a panic/recover mechanism, but generally only handles unexpected errors ).

For the return value of a function, the convention is that the last parameter returns an error object to indicate the function running status.
For example:
  1. N, err: = func ()
  2. If err! = Nil {
  3. ... // Process error
  4. }
Or write it together
  1. If n, err: = func (); err! = Nil {
  2. ... // Process error
  3. }

The error object can be constructed by errors. New () or fmt. Errorf.
For example:
  1. Var pidedErr = errors. New ("Cant pided by 0 ")
Or
  1. Err: = fmt. Errorf ("% d cant pided by 0", arg)

Let's take a look at the type of error.
Error is defined as an interface type in the standard library. this interface has only one Error () method:
  1. Type error interface {
  2. Error () string
  3. }
That is to say, the custom structure only requires the Error () method, which is equivalent to implementing the error interface.


We can create a struct and implement the Error () method to construct an error object as needed.
For example:
 
 
  1. type pision struct {
  2. arg int
  3. str string
  4. }
  5. func (e *pision) Error() string {
  6. return fmt.Sprintf("%d %s", e.arg, e.str)
  7. }
  8. func pideCheck(arg1, arg2 int) (error) {
  9. if arg2 == 0 {
  10. return &pision{arg1, "can't pided by 0"}
  11. }
  12. return nil
  13. }

Let's look at an example to check whether a group of data cannot be divided (that is, the division number is 0). If yes, an error is returned.
The code is as follows:
 
 
  1. package main
  2. import "fmt"
  3. func pideCheck(arg1, arg2 int) (error) {
  4. if arg2 == 0 {
  5. return fmt.Errorf("%d can't pided by 0", arg1)
  6. }
  7. return nil
  8. }
  9. func main() {
  10. var err error
  11. err = pideCheck(4, 2)
  12. if err != nil {
  13. fmt.Println(err)
  14. return
  15. }
  16. err = pideCheck(8, 0)
  17. if err != nil {
  18. fmt.Println(err)
  19. return
  20. }
  21. }

We have implemented this function, but the code is not elegant. every execution of the evaluate function call should use at least three lines for error handling.

Here we will optimize it. The function we need to implement is that an error is returned as long as one number cannot be divided. You only need to store the status after each check to the internal status variable, and then check the variable after all processing is complete.
The code is as follows:
 
 
  1. package main
  2. import "fmt"
  3. type pision struct {
  4. err error
  5. }
  6. func (this *pision)DivideCheck(arg1, arg2 int) {
  7. if this.err != nil {
  8. return
  9. }
  10. if arg2 == 0 {
  11. this.err = fmt.Errorf("%d can't pided by 0", arg1)
  12. return
  13. }
  14. }
  15. func (this *pision)Err() error {
  16. return this.err
  17. }
  18. func main() {
  19. d := new(pision)
  20. d.DivideCheck(4, 2)
  21. d.DivideCheck(8, 0)
  22. if d.Err() != nil {
  23. fmt.Println(d.Err())
  24. }
  25. }
This code is much more elegant. before each check, you can determine whether the internal status has encountered an error and immediately return the error, with almost no performance loss.


Golang's error handling is often criticized, but if you know how to program in go mode, you can still do very elegantly ~

Zoogolang does not have an exception handling mechanism such as try/catch. it can only rely on the return value to determine whether the status is wrong (of course, it has a panic/recover mechanism, but generally only...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.