Welcome To Golang By Example

Runtime Error Panic in Go (Golang)

Table of Contents

Overview

Runtime error in the program can happen in the below cases. All the below cases will also create a panic

Example

Let’s see an example of runtime error caused by out of bounds array access.

package main

import "fmt"

func main() {

	a := []string{"a", "b"}
	print(a, 2)
}

func print(a []string, index int) {
	fmt.Println(a[index])
}

Output

panic: runtime error: index out of range [2] with length 2

goroutine 1 [running]:
main.checkAndPrint(...)
        main.go:12
main.main()
        /main.go:8 +0x1b
exit status 2

In the above program, we have a slice of length 2 and we are trying to access slice at index 2 in the print function. Out of bound access is not allowed and it will create panic as seen from the output. Notice that in the output there are two things

There are many more cases in which runtime error can happen in a program. We are not going to mention all of them but you get the idea