我有以下代码用于在给定 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
}
但是,当我尝试运行我的代码来查找项目对时,出现以下错误:
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 和下面的="">--!sc><--!main>--!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 工作正常,没有任何问题。
最佳答案
看来您可能在某处发生了数据竞争。尝试使用 -race 标志运行它。
go run -race myfile.go
关于Golang panic : runtime error: index out of range only happens when run outside debugger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31691351/
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
RuntimeError:CUDAerror:device-sideasserttriggered问题描述解决思路发现问题:总结问题描述当我在调试模型的时候,出现了如下的问题/opt/conda/conda-bld/pytorch_1656352465323/work/aten/src/ATen/native/cuda/IndexKernel.cu:91:operator():block:[5,0,0],thread:[63,0,0]Assertion`index>=-sizes[i]&&index通过提示信息可以知道是个数组越界的问题。但是如图一中第二行话所说这个问题可能并不出在提示的代码段
我尝试通过以下命令在我的计算机上安装gem(Mechanize):>>geminstallmechanize--platform=ruby>>geminstallmechanize错误ERROR:Errorinstallingmechanize:ERROR:Failedtobuildgemnativeextension."C:/ProgramFiles/Ruby200-x64/bin/ruby.exe"extconf.rbC:/ProgramFiles/Ruby200-x64/bin/ruby.exe:invalidswitchinRUBYOPT:-F(RuntimeError)在我尝
在我们的rails3.1.4应用程序中,rspec用于测试应用程序Controller中的公共(public)方法require_signin。这是require_signin方法:defrequire_signinif!signed_in?flash.now.alert="Loginfirst!"redirect_tosignin_pathendend这是rspec代码:it"shouldinvokerequire_signinforthosewithoutlogin"docontroller.send(:require_signin)controller{shouldredirec
我正在使用Ubuntu11.04在RubyonRails中开发一个应用程序。在应用程序中,我需要生成pdf文档。所以我正在使用wicked_pdf和wkhtmltopdf二进制gem。在我系统的开发环境中,一切正常。但是一旦我使用Phusion在CentOS5.6上部署应用程序乘客,当我尝试动态生成pdf时,出现以下错误:RuntimeError(wkhtmltopdf位置未知)我正在使用Ruby1.9.2.p136rails3.1.1任何帮助将不胜感激......谢谢。 最佳答案 另一种方法是通过Gemfile安装二进制文件。只需
我正在将开发中的应用程序从Rails4.2升级到Rails5beta1.1。应用程序在升级前运行良好。我已经完成了基本的升级步骤(更新Ruby、更新Rails和相关步骤:http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html)。Gemfile也已更新为最新的Gems。当我运行$railsmiddleware或$railsconsole或$railsserver时,出现以下错误:Nosuchmiddlewaretoinsertafter:ActionDispatch::ParamsParser.../.rvm/gems
当我在Windows764位系统上运行bundleexecrspecspec/时,我收到以下错误:invalidswitchinRUBYOPT:-F(RuntimeError)我正在运行ruby1.9.2p136(2010-12-25)[i386-mingw32](安装在c:\ProgramFiles(x86)\Ruby192)和bundler1.0.15(作为rubygem安装).关于如何解决这个问题的任何线索?谢谢,本 最佳答案 Bundler不喜欢Ruby的路径包含空格这一事实。为了解决这个问题,我编辑了runtime
今天升级到Ruby-1.9.3-p392后,REXML在尝试检索超过一定大小的XML响应时抛出运行时错误-一切正常,当接收到25条以下的XML记录时不会抛出错误,但是一旦达到特定的XML响应长度阈值,我收到此错误:Erroroccurredwhileparsingrequestparameters.Contents:RuntimeError(entityexpansionhasgrowntoolarge):/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/rexml/text.rb:387:in`blockinunnormalize'我意识到这在最
我是ruby的新手,我正在尝试执行规范,但收到此错误:RuntimeError: :jsonisnotregisteredonFaraday::Request我正在尝试使用这个gem:https://github.com/Chicago/windyMacOSX10.7.4Ruby1.9.3,使用RVM 最佳答案 我刚刚在升级Faraday时遇到了这个问题。json请求中间件在0.8中从Faraday中移除。它现在位于faraday_middlewaregem中。您应该geminstallfaraday_middl
我最近从v4.3升级到Rails5.1,现在在运行测试时遇到这个错误:Anerroroccurredwhileloading./spec/controllers/admin/capacity_charges_controller_spec.rb.Failure/Error:requireFile.expand_path('../../config/environment',__FILE__)RuntimeError:can'tmodifyfrozenArray我为每个测试文件都得到了它。触发错误的行来自rails_helper。我已经检查了Rails5.1示例repo协议(protoc