Golang panic: 运行时错误: 索引超出范围,只会在在调试器外运行时发生。

huangapple go评论84阅读模式
英文:

Golang panic: runtime error: index out of range only happens when run outside debugger

问题

我有以下代码,用于在给定的切片中找到两个整数,使它们的和等于给定的总和:

type Store_object struct {
    C      int
    I      int
    Prices []int
}
//..其他无关的函数...

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
}

然而,当我尝试运行代码来找到物品对时,我遇到了以下错误:

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

(相关行在上面用<--!sc标注,下面用<--!main标注)

为了尝试调试,我使用了以下项目https://github.com/mailgun/godebug进行测试,并发现我的代码可以正常执行。

我目前不知道如何访问超出范围的值,但不知道如何进一步调试...

对此的任何指导将不胜感激!

为了更好地理解背景,这是我正在尝试实现的代码竞赛:https://code.google.com/codejam/contest/351101/dashboard#s=p0

编辑:为了提供更多背景信息,这是我正在运行的主文件,它调用了这个函数:

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没有任何问题,可以正常工作。

英文:

I have the following code used to find two integers that sum 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 &lt; len(scenarios); scIndex++ {
	    scenario := scenarios[scIndex]
	    for prIndex := 0; prIndex &lt; len(scenario.Prices); prIndex++ { //&lt;--!sc
		    firstItem := scenario.Prices[prIndex]
		    if firstItem &gt;= scenario.C {
			    continue
		    }
		    for cmpIndex := prIndex + 1; cmpIndex &lt; len(scenario.Prices); cmpIndex++ {
			    secondItem := scenario.Prices[cmpIndex]

			    switch {
			    case secondItem &gt;= scenario.C:
				    continue
			    case firstItem+secondItem == scenario.C:
				    result := &quot;Case #&quot; + strconv.Itoa(scIndex+1) +
					    &quot; &quot; + strconv.Itoa(firstItem) + &quot; &quot; +
					    strconv.Itoa(secondItem)
				    results = append(results, result)
			    }
		    }
	    }
   }
   return results, nil
}

however, when I attempt to run my code to find the item pairs, I get the following error:

panic: runtime error: index out of range

goroutine 1 [running]:
   &lt;store_credit&gt;.FindItemPairs(0x208208000, 0x1e, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0)
&lt;store_credit_location&gt;.go:76 +0x4ac 
main.main()
	&lt;main_dir&gt;/test_sc.go:16 +0x24d
exit status 2

(relevant line is noted with <--!sc above and <--!main below)

In order to try to debug, I used the following project https://github.com/mailgun/godebug to test, and found that my code executed without issue.

I currently have no clue how I would be accessing a value that is out-of-range, but have no idea how I will debug this further...

Any guidance on this would be greatly appreciated!

For more context, here is the code jam 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 I am running that calls this function:

func main() {
    cases, err := store_credit.ReadLines(&quot;A-small-practice.in&quot;)
    if err != nil {
    	fmt.Println(err)
    }

    fmt.Println(cases)

    results, err := store_credit.FindItemPairs(cases) //&lt;--!main
    if err != nil {
    	fmt.Println(err)
    }

    for i := 0; i &lt; len(results); i++ {
    	fmt.Println(results[i])
    }
}

ReadLines works fine with no issues.

答案1

得分: 6

看起来你可能在某个地方出现了数据竞争。尝试使用-race标志来运行它。

go run -race myfile.go

英文:

It appears that you may have a data race somewhere. Try running it with the -race flag.

> go run -race myfile.go

huangapple
  • 本文由 发表于 2015年7月29日 12:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/31691351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定