func Errorf(format string, a ...interface{}) error<\/code><\/pre>\n\n\n\nAs you can see it returns an instance of the error.<\/p>\n\n\n\n
Errorf<\/strong> is also a variadic function meaning that it can have multiple arguments.\u00a0There are two important points about its argument list<\/p>\n\n\n\n- Notice that the first argument is a format<\/strong> or template<\/strong>\u00a0string.<\/li><\/ul>\n\n\n\n
- The next is a variable number of arguments. Each of the arguments in this list could be string, int, struct, or anything. That is why it is an empty interface<\/li><\/ul>\n\n\n\n
Errrof<\/strong> formats the string using custom specifiers. The first argument that is the format<\/strong> or template<\/strong> string contains the actual string that needs to be formatted plus some formating verbs. These formating verbs tell how the trailing arguments will be formatted in the final string.\u00a0 So basically the format string argument contains certain symbols which are replaced by trailing arguments.\u00a0<\/p>\n\n\n\nEg\u00a0<\/p>\n\n\n\n
<\/span>Formatting a string variable<\/strong><\/span><\/h3>\n\n\n\n- %s<\/strong> symbol is used<\/li><\/ul>\n\n\n\n
- Example\u00a0<\/li><\/ul>\n\n\n\n
name := \"J\"\nfmt.Errorf(\"Name has less then 3 character. Name: %s\\n\", name)<\/code><\/pre>\n\n\n\n<\/span>Formatting an integer<\/strong><\/span><\/h3>\n\n\n\n- %d<\/strong> symbol is used<\/li><\/ul>\n\n\n\n
- Example\u00a0<\/li><\/ul>\n\n\n\n
age := 0\nfmt.Errorf(\"Age is 0: Age:%d\\n\", age)<\/code><\/pre>\n\n\n\n<\/span>Formatting a struct<\/strong><\/span><\/h3>\n\n\n\nFor example, there are three format specifiers for printing a struct. <\/p>\n\n\n\n
- %v<\/strong>\u00a0\u2013 It will print only values. The field name will not be printed. This is the default way of printing a struct when using Println<\/li><\/ul>\n\n\n\n
- %+v \u2013\u00a0<\/strong>It will print both field and value.\u00a0<\/li><\/ul>\n\n\n\n
- %#v \u2013\u00a0<\/strong>It will print the struct, also both field name and value<\/li><\/ul>\n\n\n\n
That is why<\/p>\n\n\n\n
fmt.Errorf(\"Employee not found. Details: %v\\n\", e)\nfmt.Errorf(\"Employee not found. Details: %+v\\n\", e)\nfmt.Errorf(\"Employee not found. Details: %#v\\n\", e)<\/code><\/pre>\n\n\n\nreturns an error with below formatted message respectively<\/p>\n\n\n\n
Here is the working program for the same<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n)\n\ntype employee struct {\n\tName string\n\tAge int\n}\n\nfunc main() {\n\tsampleErr := \"database connection issue\"\n\n\terr := fmt.Errorf(\"Err is: %s\", sampleErr)\n\tfmt.Println(err)\n\n\tport := 8080\n\n\terr = fmt.Errorf(\"Err is: %s to port %d\", sampleErr, port)\n\tfmt.Println(err)\n\n\te := employee{\n\t\tName: \"John\",\n\t}\n\n\terr = fmt.Errorf(\"Employee not found. Details %v\", e)\n\tfmt.Println(err)\n\terr = fmt.Errorf(\"Employee not found. Details %+v\", e)\n\tfmt.Println(err)\n\terr = fmt.Errorf(\"Employee not found. Details %#v\", e)\n\tfmt.Println(err)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nErr is: database connection issue\nErr is: database connection issue to port 8080\nEmployee not found. Details {John 0}\nEmployee not found. Details {Name:John Age:0}\nEmployee not found. Details main.employee{Name:\"John\", Age:0}<\/code><\/pre>\n\n\n\nNotice that in below Errorf<\/strong><\/p>\n\n\n\nerr = fmt.Errorf(\"Err is: %s to port %d\", sampleErr, port)<\/code><\/pre>\n\n\n\n- %s <\/strong>is replaced by sampleErr<\/strong>.<\/li><\/ul>\n\n\n\n
- %d<\/strong> is replaced by port<\/strong>.<\/li><\/ul>\n\n\n\n
So basically the symbols or verbs in the format string argument are replaced by trailing arguments in order<\/p>\n\n\n\n
If the number of format specifiers in the format string does not match the number of next variable arguments then the format specifier will be printed as is. For example, in the below code, we have two format specifier<\/p>\n\n\n\n
- %s<\/li><\/ul>\n\n\n\n
- %d<\/li><\/ul>\n\n\n\n
While the next variable number of arguments is only one. Hence when we format it then it returns the formatted error with second format specifier as is with MISSING as a warning<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n\tname := \"John\"\n\n\terr := fmt.Errorf(\"Employee not found with name: %s and age %d\", name)\n\tfmt.Println(err)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEmployee not found with name: John and age %!d(MISSING)<\/code><\/pre>\n\n\n\n