by- Kabeer Shaikh

Originally published at RemotePanda Blogs

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 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 an 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 https://blog.golang.org/errors-are-values

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,

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 like this –

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.

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.

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()

The FatalIn will show complete error along with date and time

Panic

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.

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

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 the custom errors for user understanding.

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

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

About Kabeer Shaikh:

Kabeer Shaikh is a Android Developer At MindBowser Info Solutions

About RemotePanda:

RemotePanda is a personalized platform for companies to hire remote talent and get the quality work delivered from our city Pune. All talents associated with us are close network connections. When we connect them with you, we make sure to manage their quality, their growth, the legalities and also the delivery of work. The idea is to make remote work successful for you.

RemotePanda- Make Remote Work For You

Stay connected with us on Facebook, Twitter, LinkedIn and YouTube

Also, Read our blogs here and watch our webinars.

--

--