php editor Apple brings you an introduction on how to calculate connection errors in Go 1.20 version. In the new version, the Go language provides a more precise connection error calculation method, which can better monitor and handle network connection anomalies. Through accurate calculation of connection errors, developers can locate and solve network connection problems faster, improving program stability and performance. This article will introduce in detail the principle and usage of connection error calculation in Go 1.20 version to help developers better deal with network connection anomalies.
As far as I understand, errors.Join()
is an out-of-the-box replacement for multi-error collection types, such as hashcorp/multierror, which collects parallel An error occurred. This issue does not resolve general errors in wrapping/unfolding.
When I replaced the production code, some tests failed when trying to calculate connection errors. Both the number and type of errors are important in managing the business case. This works for hashcorp/multierror as a certain string is generated such as 3 Error occurred
.
package main import "errors" func doSomething() error { var multierror error err1 := errors.New("Hello") multierror = errors.Join(err1, err1) err2 := errors.New("World") multierror = errors.Join(err2, err1) return multierror } // somewhere else func test_doSomething(t *testing.T) { actual := doSomething() assert.Len(t, 2, len(actual)????) // help }
How to count the number of errors after using errors.Join()
?
I can't find a way to check the nature of the error slice inside the connection error, and checking said error slice itself is not possible since it is private.
Using errors.Unwrap(err)
might be a way, although all I get is an error or nil
(if it's the last one). It doesn't quite let us solve counting errors except errors.As/.Is
.
errors.Join()
Record:
Therefore errors.Join()
the error
returned will have an Unwrap() []error
method. Type Assertion Holds the interface type for this method, you can call it and it will return a connected slice error:
For example:
err1 := errors.New("err1") err2 := errors.New("err2") err := errors.Join(err1, err2) if uw, ok := err.(interface{ Unwrap() []error }); ok { errs := uw.Unwrap() fmt.Println("Number of joined errors:", len(errs)) }
This will output (try it on Go Playground):
Number of joined errors: 2
The above is the detailed content of How to calculate connection errors in Go 1.20?. For more information, please follow other related articles on the PHP Chinese website!