golang

How to Use errors.New() Function in Golang – Examples

As a developer, dealing with errors is a common task as this will help you write robust and reliable code. The good thing about Go language is that it has a built-in package for error handling called error.New() function that allows you to create custom error messages on the fly, making it a valuable tool for debugging and troubleshooting your programs.

This guide is going to explore the use of error.New() function in Go with examples.

What is errors.New() Function in Golang

The errors.New() is a built-in function in Go used to create a new error message as it takes a string as an argument and returns an error type representing a new error message with the provided string as its content.

When working with Go, it is essential to handle errors correctly. Error handling helps you ensure that your code behaves as expected and gracefully handles unexpected situations. The errors.New() function allows you to create custom error messages to handle unexpected scenarios in your code.

A simple syntax can be seen below:

errors.New(message)

Here message is the error message that you would like to display to the user.

How to Use errors.New() Function?

Here are the steps involved to use errors.New() function in Go:

Step 1: First, you have to import the errors package into your Go program by including the following line of code at the top of your file:

import "errors"

Step 2: Next, call the errors.New() function to generate a new error. An error message you’d like to display is included in a string that is passed as an argument to the function. For example:

err := errors.New("file not found")

Step 3: Handle the error in your code using an if statement. For example:

if err != nil {

// handle the error

}

Here’s an example of how to use errors.New() function in Go:

package main

import (

  "errors"

  "fmt"

)

func main() {

  msg := "Greetings"

  Error := errors.New("Invalid MESSAGE")

if msg != "Linuxhint" {

fmt.Println(Error)

}

}

In the above example, we used the errors.New() function to create an error. The custom error message is “Invalid MESSAGE” inside the error.New() function in this case.

Here’s another example:

package main

import (

  "errors"

  "fmt"

)

func divide(a, b int) (int, error) {

  if b == 0 {

    return 0, errors.New("division by zero")

  }

  return a / b, nil

}

func main() {

  result, err := divide(10, 0)

  if err != nil {

fmt.Println("Error:", err)

    return

  }

fmt.Println("Result:", result)

}

In the above example, we define a divide() function that takes two integers as arguments and returns their division. If the second argument b is 0, we return an error using errors.New() function with a custom error message “division by zero”. We call the division() function with the arguments 10 and 0 into the main function, which results in an error. Then, when the error is not nil, we check to see if it is null and print the message. If the error is nil, we print the result.

Conclusion

The errors.New() function in Go is useful for handling errors and creating custom error messages in your code. By following the simple steps outlined in this guide and the examples provided, you can effectively use this function to debug and troubleshoot your programs. With proper error handling, you can write more robust and reliable code, making your programs more stable and user-friendly.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.