douyudouchao6779 2018-04-08 17:10
浏览 103
已采纳

分割切片时出现“切片超出范围”错误

I want to run a bunch of goroutines to insert an insanely large amount of .csv-files into a postgres database. I read the .csv-files to an object and then try to divide the object in 10 parts. This works perfectly when the amount of files is exactly dividable by 10, but when it's not I get a panic: runtime error: slice bounds out of range.

This would be sensible if i + len(fis)/gophers returned a float, but it does not. When I use the debgging-mode of Goland-IDE, both i as well as the result of i + len(fis)/gophers are full integers.

Any clue clue as to why this error occurs? I'm mostly interested in the theory behind why this happens, but also curious if someone can come up with a solution other than using the %-operator.

Thanks in advance!

Edit: I was under the impression that the error returned on the first iteratio (at which point, according to the debugger both i and i + len(fis)/gophers are well within the bounds of len(fis). The amount of files in the directory is 2568. It turns out the error was on the last iterattion. Thanks Berry for pointing this out.

//Read files from download-folder
f, _ := os.Open(dlFolder)
fis, _ := f.Readdir(-1)
f.Close()

var wg sync.WaitGroup

gophers := 10

//Split slice of files into 10 chunks and run a goroutine for each of them
for i := 0; i < len(fis) ; i = i + len(fis)/gophers{

    wg.Add(1)

    xFiles := fis[i:i + len(fis)/gophers]
    go csvTSql(xFiles)

}

wg.Wait()
  • 写回答

1条回答 默认 最新

  • duangu8264 2018-04-08 17:31
    关注

    Check again. i + len(fis)/gophers will not be within bound if len(fis) is not evenly divisible by gophers. It will exceed the bounds on the last iteration.

    There are a number of ways to fix this like checking if i + len(fis)/gophers > len(fis) - 1 and resetting the value if it is. You can also just use a math.Min call.

    chunk := int(math.Min(float64(i+len(fis)/gophers), float64(len(fis))))
    xFiles := fis[i:chunk]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?