Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

go

What is type error in Golang?

Khizar Hayat Saani

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

error is a built-in interface type in Go.

An error variable represents any value that can describe itself as a string.

The following is the error interface declaration:

type error interface {
    Error() string
}

The interface consists of a single function, Error(), that returns a string error message.

Use

Go does not provide support for conventional try-and-catch block for error handling. Instead, when something unexpected happens, it returns an error.

We use the error interface to represent the error condition. A nil value means that no error has occurred.

To generate custom errors, we can implement the error interface in a struct, as per our requirements.

type DivZero struct{}
func (myerr *DivZero) Error() string{
  return "Cannot divide by 0!"
}
Compilers returns an error on unexpected behavior

The errors package

To generate custom errors, we need to define a struct that implements the error interface. This is a rather tedious task.

To avoid defining structs for error handling, Go provides the built-in errors package.

The errors package exports a function called New. This function takes an error message as input and returns an error type.

func divide(x int, y int) (int, error) {
	if y == 0 {
    return -1, errors.New("Cannot divide by 0!")
  }
  return x/y, nil
}

Code

package main
import "fmt"
import "os"
import "errors"
//custom error --> struct
type DivZero struct{}
func (myerr *DivZero) Error() string{
return "Cannot divide by 0! (struct)"
}
//custom error --> New
func divide(x int, y int) (int, error) {
if y == 0 {
return -1, errors.New("Cannot divide by 0! (New)")
}
return x/y, nil
}
func main(){
// basic use --> trying to open non existing file
filename, err := os.Open("Nofile.txt")
if err != nil {
fmt.Println(err)
}
fmt.Println(filename) //comes out as <nil>
answer, err := divide(5,0)
if err != nil {
// Handle the error!
fmt.Println(err)
} else {
// No errors!
fmt.Println(answer)
}
myerr := &DivZero{}
fmt.Println(myerr)
}

In the above example, we explore all options are associated with the use of the type error.

We begin by trying to open a file that does not exist. We are then dealt with an error and the error message is printed.

Next, we move on to custom errors. The divide by zero exception is handled using the New export in the errors package.

Finally, we create a struct DivZero. The Println() function automatically calls its Error() function and the error message is printed.

RELATED TAGS

go

CONTRIBUTOR

Khizar Hayat Saani
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring