0
0
GoHow-ToBeginner · 3 min read

How to Create Error in Go: Simple Guide with Examples

In Go, you create an error by using the errors.New function from the errors package or by implementing the error interface yourself. The errors.New function returns a simple error with a message, which you can return or handle in your code.
📐

Syntax

To create a basic error in Go, use the errors.New function which takes a string message and returns an error type. You can also create custom errors by defining a type that implements the Error() method.

  • errors.New("message"): creates a new error with the given message.
  • type MyError struct { Msg string }: defines a custom error type.
  • func (e MyError) Error() string: method to satisfy the error interface.
go
import "errors"

// Create a simple error
err := errors.New("this is an error message")

// Custom error type example
type MyError struct {
    Msg string
}

func (e MyError) Error() string {
    return e.Msg
}
💻

Example

This example shows how to create and use a simple error with errors.New and how to create a custom error type with a message.

go
package main

import (
    "errors"
    "fmt"
)

type MyError struct {
    Msg string
}

func (e MyError) Error() string {
    return e.Msg
}

func mightFail(fail bool) error {
    if fail {
        return errors.New("simple error occurred")
    }
    return nil
}

func mightFailCustom(fail bool) error {
    if fail {
        return MyError{Msg: "custom error occurred"}
    }
    return nil
}

func main() {
    err := mightFail(true)
    if err != nil {
        fmt.Println("Error from mightFail:", err)
    }

    err = mightFailCustom(true)
    if err != nil {
        fmt.Println("Error from mightFailCustom:", err)
    }
}
Output
Error from mightFail: simple error occurred Error from mightFailCustom: custom error occurred
⚠️

Common Pitfalls

Common mistakes when creating errors in Go include:

  • Returning nil instead of an error when an error should be returned.
  • Not implementing the Error() method correctly for custom errors.
  • Using plain strings instead of errors, which loses error type information.

Always return an error type and check for nil before using the error.

go
package main

import (
    "errors"
    "fmt"
)

// Wrong: returning string instead of error
func wrong() string {
    return "error happened"
}

// Right: returning error
func right() error {
    return errors.New("error happened")
}

func main() {
    // This will not work as expected
    // err := wrong() // err is string, not error

    err := right()
    if err != nil {
        fmt.Println("Got error:", err)
    }
}
Output
Got error: error happened
📊

Quick Reference

Summary tips for creating errors in Go:

  • Use errors.New("message") for simple errors.
  • Define custom error types by implementing the Error() method.
  • Always return error type, not strings.
  • Check errors with if err != nil before proceeding.

Key Takeaways

Use errors.New to create simple error messages in Go.
Custom errors require implementing the Error() string method.
Always return and check error types, not plain strings.
Check for nil before using an error to avoid runtime issues.