Error Handling in Golang

Error Handling in Golang

error handling in golang


Error Handling

In Chapter-6 of our Golang Tutorial, we touched upon Concurrency in Golang. In this chapter, let’s explore ‘Error Handling’.

Whenever you come across any runtime problem while executing your Go application that is an error. Go doesn’t have an exception but in Go, it is in the form of panic and recover. Go has a simple and easy way of handling when something goes wrong in the program. Basically, Go is having the ability to return multiple values.

By convention, if something goes wrong then the function returns an error as its last return value.


func sayHello(msg string) (string,error){

//your code goes here}


An error type is an interface which is there in errors package of Golang.


type error interface{ Error( ) string }


This means any type that implements Error( ) returns a string satisfying error interface. The string returned gives a message so that we can find what went wrong.



Here err!=nil means err value is having some values. So, if err contains some values that means an error occurred there and we need to handle this.

In Go, error handling is very important. As in above example, we have to check error every time.


In Go, function passes error just like types and as per Robert Pike’s blog errors are valued

An error in Go must either be –

  • handled/ignored right there, or

  • returned to caller

For example, the os.Open() returns non-nil value when fails to open file.


func Open(name string)(file *File,err error)


Find the below example –


Here, above if os.Open( ) fails to open a file then it will return err with the message like,


error message


Here in the below example of web service, if some unexpected server-side error were to happen, we would generate 5xx error. So you have to do this –

server side error


Here we have to repeat the same error handling code across all of our handler functions.

The most commonly used error’s implementation is the error package’s unexported type errorString.

error string


You can construct one of these values with the errors. New function. It takes a string that it converts to an errors.errorString and returns as an error value.

error value


Here’s how you might use errors. New:



A caller passing a negative argument to Sqrt receives a non-nil error value (whose concrete representation is an errors.errorString value). The caller can access the error string (“math: square root of…“) by calling the error’s Error method, or by just printing it:

Errors Logging to File

The Golang has given standard library package “log” for implementing logging and that will write the standard error and also the data and time of each logged message.



Here the log.txt file is created with error information.

log.Println() calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.

Fatalln is also same as Println followed by a call to os.Exit()

golang error


The FatalIn will show complete error along with date and time


A panic means something went wrong unexpectedly. Mostly we use it to fail fast on errors that shouldn’t occur during normal operations.

We’ll use panic to check for unexpected errors. A panic is used to abort if a function returns an error value that we don’t know how to handle.

func main()


After running this program, if we get an error, it will cause it to panic and returns an error message and goroutine traces and exits with nonzero status.



Panic is good if you want to see some of the stacks.


Recover is only useful inside deferred functions. During normal execution, a call to recover will return nil and have no other effect. If the current goroutine is panicking, a call to recover will capture the value given to panic and resume normal execution.



Custom Errors

“Error values in Go aren’t special, they are just values like any other, so you have entire language at your disposal”.

So we can create custom errors for user understanding.

In the “errors” package Go has provided a func New(text string)error for manipulating errors.

custom errors
custom error


A struct is defined by using type and struct keyword along with a meaningful name

As we have seen ‘Error Handling’ in this chapter, let’s move to the next one and see Common Utilities in Golang Project


Leave a Reply