Error handling errors of Golang
There is no exception-handling mechanism in Golang that can only rely on the return value to make a state error (of course it has a panic/recover mechanism, but generally only handles unexpected errors).
For a function's return value, the Convention is that the last parameter returns an Error object to indicate the state of the function's run.
Such as:
- N, Err: = Func ()
- If err! = Nil {
- ....//process Error
- }
or write it together.
- If n, err: = Func (); Err! = Nil {
- ....//process Error
- }
The error object can be made by errors. New () or FMT. Errorf () construction.
Such as:
- var dividederr = errors. New ("Cant divided by 0")
Or
- ERR: = FMT. Errorf ("%d cant divided by 0", Arg)
Let's start by looking at what the error is.
Error is defined as an interface type in the standard library, which has only one error () method:
- Type Error Interface {
- Error () string
- }
In other words, the custom structure only needs to have the error () method, which is equivalent to implementing the error interface.
We can create a struct and implement the error () method to construct the error object according to our own wishes.
Such as:
- Type Division struct {
- ARG int
- STR string
- }
- Func (e *division) Error () string {
- Return to FMT. Sprintf ("%d%s", E.arg, E.str)
- }
- Func Dividecheck (arg1, arg2 int) (error) {
- if arg2 = = 0 {
- Return &DIVISION{ARG1, "can ' t divided by 0"}
- }
- return Nil
- }
Another example is to check if there is a set of data that cannot be removed (that is, the divisor is 0), and if so, an error is returned.
The code is as follows:
- Package Main
- Import "FMT"
- Func Dividecheck (arg1, arg2 int) (error) {
- if arg2 = = 0 {
- Return to FMT. Errorf ("%d can ' t divided by 0", arg1)
- }
- return Nil
- }
- Func Main () {
- var err error
- Err = Dividecheck (4, 2)
- If err! = Nil {
- Fmt. PRINTLN (ERR)
- Return
- }
- Err = Dividecheck (8, 0)
- If err! = Nil {
- Fmt. PRINTLN (ERR)
- Return
- }
- }
We implemented this feature, but the code is very elegant, with at least 3 lines of error handling per function call execution.
below to optimize. The function we need to implement is to return an error whenever there is a number that cannot be removed. Then you only need to store the state after each check in the internal state variable, and then check the variable after all processing is done.
The code is as follows:
- Package Main
- Import "FMT"
- Type Division struct {
- Err error
- }
- Func (this *division) Dividecheck (arg1, arg2 int) {
- If This.err! = Nil {
- Return
- }
- if arg2 = = 0 {
- This.err = FMT. Errorf ("%d can ' t divided by 0", arg1)
- Return
- }
- }
- Func (this *division) ERR () error {
- Return This.err
- }
- Func Main () {
- D: = new (Division)
- D.dividecheck (4, 2)
- D.dividecheck (8, 0)
- If D.err ()! = Nil {
- Fmt. Println (D.err ())
- }
- }
This code is much more elegant, and before each check to determine whether the internal state has been wrong, the error is immediately returned, almost no performance loss.
Golang's error handling is often criticized, but if you know how to program it in go, you can do it gracefully.
http://www.bkjia.com/PHPjc/1114325.html www.bkjia.com true http://www.bkjia.com/PHPjc/1114325.html techarticle Golang error Handling Errors Golang There is no try/catch such an exception handling mechanism, can only rely on the return value to do State error judgment (of course, it has a panic/recover mechanism, but generally only ...)