首页 练字文章 Effective Error Handling in Golang 0

Effective Error Handling in Golang 0

2022-06-13 13:40  浏览数:310  来源:mizumoto    

Effective Error Handling in Golang
Brandon Schurman
Error handling in Go is a little different than other mainstream programming languages
like Java, JavaScript, or Python. Go’s built-in errors don’t contain stack traces,
nor do they support conventional try/catch methods to handle them.
Instead, errors in Go are just values returned by functions, and they can
be treated in much the same way as any other datatype - leading to a surprisingly
lightweght and simple design.In this article, I’ll demonstrate the basics of
handling errors in Go, as well as some simple strategies you can follow
in your code to ensure your program is robust and easy to debug.
The Error TypeThe error type in Go is implemented as the following interface:
type error interface {
Error() string
}
So basically, an error is anything that implements the Error() method, which
returns an error message as a string. It’s that simple!
Constructing Errors
Errors can be constructed on the fly using Go’s built-in errors or fmt packages.
For example, the following function uses the errors package to return a new
error with a static error message:
package main
import "errors"
func DoSomething() error {
return errors.New("something didn't work")
}
Similarly, the fmt package can be used to add dynamic data to the error, such
as an int, string, or another error. For example:
package main
import "fmt"
func Divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("can't divide '%d' by zero", a)
}
return a / b, nil
}
Note that fmt.Errorf will prove extremely useful when used to wrap another
error with the %w format verb - but I’ll get into more detail on that further
down in the article.
There are a few other important things to note in the example above.
- Errors can be returned as nil, and in fact, it’s the default, or “zero”, value of
on error in Go. This is important since checking if err != nil is the
idiomatic way to determine if an error was encountered (replacing the
try/catch statements you may be familiar with in other programming languages).
- Errors are typically returned as the last argument in a function. Hence in our example
above, we return an int and an error, in that order.
- When we do return an error, the other arguments returned by the function are
typically returned as their default “zero” value. A user of a function may expect
that if a non-nil error is returned, then the other arguments returned are not relevant.
- Lastly, error messages are usually written in lower-case and don’t end in
punctuation. Exceptions can be made though, for example when including
a proper noun, a function name that begins with a capital letter, etc.
Defining Expected Errors
Another important technique in Go is defining expected Errors so they can be
checked for explicitly in other parts of the code. This becomes useful when you
need to execute a different branch of code if a certain kind of error is encountered.



声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)