2. Error Handling
● Unwanted or unexpected condition occurring in program is ERROR
● E.g. File creation error, DB connection error, out of bound memory access etc.
● Error handling in Golang is unconventional as compared to that of languages like
Javascript, Java and Python
● Golang represents errors using the built-in error interface type
type error interface {
Error() string
}
● Interface contains a single method Error() that returns an error message as a string
● The error is often returned as a second argument of the function, making use of multiple
value return
3. Error Handling
● Go does not have exceptions like many other programming languages
● Go uses “Defer, panic and recover" for error handling
● A defer statement is a mechanism used to defer a function by putting it into an
executed stack once the function that contains the defer statement has finished, either
normally by executing a return statement or abnormally panicking
● A panic statement signals Golang that your code cannot solve the current problem and it
therefore stops the normal execution flow of your code
● Panics can be recovered by calling the built-in recover function within a deferred
function in the function that is panicking. Recover will then end the current state of
panic and return the panic error value
4. References
● “The Go Programming Language ”,Alan A.A.Donovan,Brian W.Kernighan,Addison Wesley Publication