I am having a somewhat inexplicable error in go where I encounter an error at a line which the program, in theory, should not be executing at that moment.
func exampleFunction() {
//cycleCount starts as 1, layerDif = 4 , composition is an int array with 5 entries
if cycleCount <= layerDif {
fmt.Println("Evaluating if")
fmt.Println("layerDif", layerDif, "cycleCount", cycleCount)
for i := 0; i < composition[cycleCount]; i++ { //this is line 68
//... random code
cycleCount++
fmt.Println("cycle++",cycleCount)
exampleFunction() // this is line 72
}
} else {
fmt.Println("Evaluating else")
//... random code
fmt.Println("Argyle")
for i := 0; i < layerDif -1; i++ {
fmt.Println("Sock", i)
//... random code
}
//... random code
}
}
This code results in an output of:
Evaluating if
layerDif 4 cycleCount 1
cycles 2
Evaluating if
layerDif 4 cycleCount 2
cycles 3
Evaluating if
layerDif 4 cycleCount 3
cycles 4
Evaluating if
layerDif 4 cycleCount 4
cycles 5
Evaluating else
panic: runtime error: index out of range
goroutine 1 [running]:
main.exampleFunction()
/home/name/Go/Predict/src/main/Filename.go:68 +0x5c1
main.exampleFunction()
/home/name/Go/Predict/src/main/Filename.go:72 +0x25e
main.exampleFunction()
/home/name/Go/Predict/src/main/Filename.go:72 +0x25e
main.exampleFunction()
/home/name/Go/Predict/src/main/Filename.go:72 +0x25e
...some other output which doesn't matter
exit status 2
I'm at somewhat of a loss for words, as the line at which the error occurs is listed as 68. However, just before this output is given, it prints "Evaluating else", an output which should be given only once line 68 is done being evaluated (The recursive function has stopped). The second mystery is why the for loop following "Evaluating else" is never executed (And should have printed a series of outputs with "Sock" in them). This is somewhat bewildering and I can't exactly pinpoint the cause of this error. Why is this occurring?