This tutorial aims to help you understand the best practices for error management in Go. Effective error handling is a crucial aspect of any programming language, and Go is no exception. By the end of this guide, you'll be able to handle, log, and debug errors efficiently in your Go programs.
What you'll learn:
Prerequisites:
Go has a simple error handling model using a separate return value. This differs from many other languages, which use exceptions. Here are some guidelines and best practices:
file, err := os.Open("file.txt")
if err != nil {
// handle the error
log.Fatal(err)
}
// use the file
if num < 0 {
return fmt.Errorf("received negative number %d", num)
}
if name == "" {
return fmt.Errorf("name cannot be empty")
}
Example 1:
// This function returns an error if the input is less than zero
func checkNegative(num int) error {
if num < 0 {
return fmt.Errorf("received negative number %d", num)
}
return nil
}
func main() {
err := checkNegative(-1)
if err != nil {
log.Fatal(err)
}
}
In this example, the checkNegative
function returns an error when it receives a negative number. The main
function then logs and exits if there is an error.
Expected output:
2009/11/10 23:00:00 received negative number -1
exit status 1
Example 2:
type CustomError struct {
Err error
Time time.Time
}
func (ce CustomError) Error() string {
return fmt.Sprintf("an error occured at %v: %v", ce.Time, ce.Err)
}
func main() {
err := CustomError{
Err: errors.New("my custom error"),
Time: time.Now(),
}
fmt.Println(err)
}
This example shows how to create custom errors. CustomError
holds an error and the time it occurred. If an error occurs, it prints the error and the time it occurred.
Expected output:
an error occured at 2009-11-10 23:00:00 +0000 UTC m=+0.000000001: my custom error
This tutorial covered how to handle, log, and debug errors in Go. We learned that errors are just values that we can design as we please. We also examined how to create custom errors for more control over error information.
Next, you could learn more about panics in Go, which are similar to exceptions in other languages. You could also study some open-source Go projects to see how they handle errors.
Exercise 1:
Write a function that returns an error if a string is empty.
Solution:
func checkEmpty(str string) error {
if str == "" {
return fmt.Errorf("string cannot be empty")
}
return nil
}
Exercise 2:
Create a custom error type that contains a time, message, and code.
Solution:
type CustomError struct {
Message string
Code int
Time time.Time
}
func (ce CustomError) Error() string {
return fmt.Sprintf("at %v, code %d: %s", ce.Time, ce.Code, ce.Message)
}
Remember, practice is key to mastering error handling in Go. Always aim to write clear, understandable error messages and handle errors gracefully.