Gonang panic: runtime error: an out-of-range index only occurs when an external debugger is started

I have the following code used to find two integers that add up to a given total in a given slice:

type Store_object struct { C int I int Prices []int } //..other unrelated functions... func FindItemPairs(scenarios []Store_object) ([]string, error) { var results []string for scIndex := 0; scIndex < len(scenarios); scIndex++ { scenario := scenarios[scIndex] for prIndex := 0; prIndex < len(scenario.Prices); prIndex++ { //<--!sc firstItem := scenario.Prices[prIndex] if firstItem >= scenario.C { continue } for cmpIndex := prIndex + 1; cmpIndex < len(scenario.Prices); cmpIndex++ { secondItem := scenario.Prices[cmpIndex] switch { case secondItem >= scenario.C: continue case firstItem+secondItem == scenario.C: result := "Case #" + strconv.Itoa(scIndex+1) + " " + strconv.Itoa(firstItem) + " " + strconv.Itoa(secondItem) results = append(results, result) } } } } return results, nil } 

however, when I try to run my code to find pairs of elements, I get the following error:

 panic: runtime error: index out of range goroutine 1 [running]: <store_credit>.FindItemPairs(0x208208000, 0x1e, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0) <store_credit_location>.go:76 +0x4ac main.main() <main_dir>/test_sc.go:16 +0x24d exit status 2 

(the corresponding line is marked with <-! sc above and below - below)

To try debugging, I used the following https://github.com/mailgun/godebug project for verification and found that my code executed without problems.

Currently I don't know how to access a value that is out of range, but I don't know how to debug this further ...

Any advice on this would be greatly appreciated!

For more context, here is an example jam that I'm trying to implement: https://code.google.com/codejam/contest/351101/dashboard#s=p0

Edit: for even more context, here is the main file that I run that calls this function:

 func main() { cases, err := store_credit.ReadLines("A-small-practice.in") if err != nil { fmt.Println(err) } fmt.Println(cases) results, err := store_credit.FindItemPairs(cases) //<--!main if err != nil { fmt.Println(err) } for i := 0; i < len(results); i++ { fmt.Println(results[i]) } } 

ReadLines works fine without a problem.

+5
source share
1 answer

It seems like you might have a data race somewhere. Try running it with the -race flag.

go run -race myfile.go

+5
source

All Articles