Golang panic: runtime error: index out of range

4.1k views Asked by At

This function takes an array with some integers and my goal is to have a new array with just positive integers:

func domath(newarray[] int, i int, array[] int)([]int){
    if i < len(newarray) {
        if newarray[i] < 0{
            i ++    
            domath(newarray, i, array)
        }
        if newarray[i] >= 0 {
            array = append(array, newarray[i])
            i ++
            domath(newarray, i, array)
        }
    }
    return array 
}

However, I keep getting the same error saying panic:

runtime error: index out of range

3

There are 3 answers

0
Vutuz On

Do you want write a recursive function?, you can see my code below :

func domath(newarray []int, i int, array []int) []int {
if i < len(array) {
    if array[i] >= 0 {
        newarray = append(newarray, array[i])
    }
    i++
} else {
    return newarray
}
return domath(newarray, i, array)

}

0
Pablo Lalloni On

The problem with that implementation is that it's incrementing i inside the first if block and then using the new i value to check newarray[i] >= 0 on the second if block, so when you call domath(a, x, b) with x = len(a)-1 it tries to do newarray[x+1] (i.e. newarray[len(newarray)]) which is out of bounds.

You probably meant to write something like:

func domath(newarray []int, i int, array []int) []int {
    if i < len(newarray) {
        if newarray[i] < 0 {
            return domath(newarray, i+1, array)
        }
        if newarray[i] >= 0 {
            array = append(array, newarray[i])
            return domath(newarray, i+1, array)
        }
    }
    return array
}

A simplified version of your algorithm could be:

func domath(newarray []int, i int, array []int) []int {
    if len(newarray) == i {
        return array
    }
    if newarray[i] >= 0 {
        array = append(array, newarray[i])
    }
    return domath(newarray, i+1, array)
}

Yet you should probably be using an implementation more idiomatic like the following one, which will also be faster:

func domath(ns []int) []int {
    var ps []int
    for _, n := range ns {
        if n >= 0 {
            ps = append(ps, n)
        }
    }
    return ps
}
0
Alperen KILIÇ On

I solved this problem. It is simple but common mistake. The golang panic error coming out because of we missing the "make a slice or array". for example

package main

import "fmt"

func main() {
    var elementNumber int
    fmt.Println("How many elements do you want in slice?")
    fmt.Scanln(&elementNumber)

    storage := make([]int, elementNumber) // this line crucial important.
    for i := 0; i < elementNumber; i++ {
        fmt.Printf("please enter %d. integer number= ", i+1)
        fmt.Scanln(&storage[i])
    }
}

If I could have do instead this and do not use "make",it would have not work ;

package main

import "fmt"

func main() {
    var elementNumber int
    fmt.Println("How many elements do you want in slice?")
    fmt.Scanln(&elementNumber)

    storage := []int{}
    for i := 0; i < elementNumber; i++ {
        fmt.Printf("please enter %d. integer number= ", i+1)
        fmt.Scanln(&storage[i])
    }
}

enter image description here