golang

Golang Fmt Errorf

The fmt package is one of the most useful packages in the Go standard library. It allows us to work with I/O operations, including printing information to the console.

The fmt package also provides you with an errorf function to generate custom and descriptive error messages. The function supports custom formatting specifiers and creates informative error messages for users and developers.

Let us learn how we can create custom error messages using the fmt.Errorf Function.

Function Syntax

The function definition is as shown:

func Errorf(format string, a ...interface{}) error

The function accepts two parameters:

  1. The format as a string containing the custom specifiers used by the function to format the actual error message.
  2. The interface contains a list of arguments to replace the custom specifier values in the error message string. You can pass any number of arguments of any time provided the specifier is set.

Format Specifiers

The following are some common specifiers to use with the errorf function:

  1. %s – Print a string.
  2. %d – print an integer
  3. %f – floating point number.
  4. %v – print values of a struct.
  5. %+v – print struct fields and values.
  6. %t – print Boolean.
  7. %b – print binary value.
  8. %e – scientific notation

These common specifiers are used with the errorf function. Check the following documentation provided to learn more.

https://pkg.go.dev/fmt

The function should return the error message generated according to the format specifier and the passed arguments.

Example 1

The following code shows how to use the errorf function to generate a custom error message:

package main
import"fmt"
funcmain() {
    const username, uid = "debian", 1000
    err := fmt.Errorf("[Error!]...Username %s of uid: %d not found.", username, uid)
    fmt.Println(err)
}

In the previous code, we create two constant variables holding a string and an int. We then use the Errorf method to generate a custom error message with the %s and %d formatters for a string and integer, respectively.

The code above should return an error message as:

[Error!]...Username debian of uid: 1000 not found.

Example 2

We can also format a struct using the %v or %+v specifiers. An example is provided below:

package main
import"fmt"
type user struct {
    first_name, last_namestring
}
funcmain() {
    user1 := user{"Jane", "Doe"}
    err := fmt.Errorf("Struct info %+v", user1)
    fmt

The previous code should print the struct field and values as shown below:

Structinfo {first_name:Janelast_name:Doe}

Example 3

We can also include the timestamp for an error message using the built-in time methods. An example is provided below:

package main
import (
    "fmt"
    "time"
)
funcmain()
    err := fmt.Errorf("An error occurred at: %v", time.Now().Format("2006-01-02T15:04:05Z07:00"))
    fmt.Println(err)
}

The previous code should include the current time formatted according to RFC3339 formatting options.

The resulting output is as shown:

An error occurred at: 2022-01-28T15:55:01+03:00

Conclusion

This guide covered the fundamentals of generating custom error messages using the fmt.Errorf function, plus several examples for review. We hope you found this article helpful. Check out the other Linux Hint articles for more tips and tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list