Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What is an error in Go language?

Samia Ishaque


The Go language has an in-built type error that is used for error handling. error simplifies identifying which function/s or code returns an error, and why the error has occurred. To use error, you must first import the errors package, as shown below:

import (

An error type can return as nil or not. If it is nil, then there is no error.


The following example will help you understand the error type better. In the example below, the user is first asked to input their age into an online screening system for job applications. After acquiring their age, we use a program to check whether it’s invalid, underage, or valid. If their age is invalid or underage, the required error is returned to indicate so. If not, the error returned is nil.

package main
import (

func check(age int)(string, error){
  if age < 0{ //code for invalide age of employee
    return "",errors.New("Invalid age")
  } else if age < 20{ //code for invalide age of employee
    return "",errors.New("Under age")
  } else { //if the age is above 20
    return "Can apply!", nil


func main() {
    //take input from user

    fmt.Println("Enter Your Age: ")
    var age int

    validity, err := check(age)
    if err != nil {
    } else {

Enter the input below

Apart from New(), there are three more functions in the error package:

Name Prototype Description
As() func As(err error, target interface{}) bool Looks for error in an error chain. Upon success, sets target to the error and returns true.
Is() func Is(err, target error) bool If the target matches the error, then the function returns true.
Unwrap() func Unwrap(err error) error Returns an error if the Unwrap() call to an error returns an error. Otherwise, it returns nil.




Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring