Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

Recover function return value in Go (Golang)

Posted on September 21, 2020September 21, 2020 by admin

Table of Contents

  • Overview
  • Example

Overview

The recover function  returns the value which was passed to the panic function. Therefore it is a good practice to check the return value of the recover function. If the return value is non nil then panic did not happen and recover function was not called with the panic.

Example

Let’s see a program to fully understand it

package main

import "fmt"

func main() {

	a := []string{"a", "b"}
	checkAndPrint(a, 2)
	fmt.Println("Exiting normally")
}

func checkAndPrint(a []string, index int) {
	defer handleOutOfBounds()
	if index > (len(a) - 1) {
		panic("Out of bound access for slice")
	}
	fmt.Println(a[index])
}

func handleOutOfBounds() {
	if r := recover(); r != nil {
		fmt.Println("Recovering from panic:", r)
	}
}

Output

Recovering from panic: Out of bound access for slice
Exiting normally

In the above program we have a function checkAndPrint which checks and prints slice element at an index passed in the argument. If the index passed is greater than the length of the array then the program panics.  We have added a defer function named handleOutIfBounds as well at the start of the function checkAndPrint.   This function  contains  the  call to recover function  as below

if r := recover(); r != nil {
    fmt.Println("Recovering from panic:", r)
}

The recover function will catch the panic and we can also print the message from the panic. 

Recovering from panic: Out of bound access for slice

After the recover function the program continues and the control returns to the called function which is  main here. That is why we get output as

Exiting normally

The recover function returns the value which was passed to the panic function. That is why we have below code in the  defer function handleOutofBounds

if r := recover(); r != nil 

Here if r is nil then panic did not happened. So if there is no panic then call to recover will return nil

  • go
  • golang
  • Follow @golangbyexample

    Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme