Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Different ways of creating an error in Go (Golang)

Posted on November 13, 2023November 13, 2023 by admin

Before learning about different ways of creating am an error in Golang lets first understand about the error. Error is an interface type with below signature

type error interface {
Error() string
}

Any by definition of interface any type which implements the Error() method becomes of type error.

Let’s see now different methods of creating an error

1. Using errors.New(“some_error_message”)

package main
import (
"errors"
"fmt"
)
func main() {
sampleErr := errors.New("error occured")
fmt.Println(sampleErr)
}

Output:

error occured

2. Using fmt.Errorf(“error is %s”, “some_error_message”). This way creates error with formatting of error messages

package main
import (
"fmt"
)
func main() {
sampleErr := fmt.Errorf("Err is: %s", "database connection issue")
fmt.Println(sampleErr)
}

Output:

Err is: database connection issue

3. Creating Custom error

The below example illustrates the use of custom error. In below example

  • inputError is a struct that has the Error() method hence it is of type error
  • You can also add additional information to the custom error by extending its fields or by adding new methods. inputError has an additional field named missingFields and a function getMissingFields function.
  • We can use type assertion to convert from error to inputError

Example:

package main
import "fmt"
type inputError struct {
message string
missingField string
}
func (i *inputError) Error() string {
return i.message
}
func (i *inputError) getMissingField() string {
return i.missingField
}
func main() {
err := validate("", "")
if err != nil {
if err, ok := err.(*inputError); ok {
fmt.Println(err)
fmt.Printf("Missing Field is %s\n", err.getMissingField())
}
}
}
func validate(name, gender string) error {
if name == "" {
return &inputError{message: "Name is mandatory", missingField: "name"}
}
if gender == "" {
return &inputError{message: "Gender is mandatory", missingField: "gender"}
}
return nil
}

Output:

Name is mandatory
Missing Field is name

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2023 Welcome To Golang By Example | Design: Web XP