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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试