安全地中断csv读写
我正在处理csv文件,当我中断该过程时,我想将未处理的数据存储到另一个文件中 这就是我所做的安全地中断csv读写,csv,go,Csv,Go,我正在处理csv文件,当我中断该过程时,我想将未处理的数据存储到另一个文件中 这就是我所做的 csvFile, err := os.Open(csvPath) r := csv.NewReader(csvFile) sigc := make(chan os.Signal, 1) signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) go func
csvFile, err := os.Open(csvPath)
r := csv.NewReader(csvFile)
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
<-sigc
savePending(r)
}()
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Println(record, err)
continue
}
doSomethingWithRecord(record)
}
但是当我运行代码时,然后按CTRL+C来中断脚本,我总是感到恐慌
panic: runtime error: slice bounds out of range [:7887] with capacity 4096
goroutine 82 [running]:
bufio.(*Reader).ReadSlice(0xc0000c2ea0, 0x105930a, 0x88, 0x90, 0xc00090cab0, 0x0, 0x0)
/usr/local/Cellar/go/1.13.3/libexec/src/bufio/bufio.go:334 +0x232
encoding/csv.(*Reader).readLine(0xc00015c1b0, 0x9, 0x9, 0xc00090cab0, 0xc00090f680, 0x20e)
/usr/local/Cellar/go/1.13.3/libexec/src/encoding/csv/reader.go:218 +0x49
encoding/csv.(*Reader).readRecord(0xc00015c1b0, 0x0, 0x0, 0x0, 0xc00090cab0, 0x9, 0x9, 0x0, 0x0)
/usr/local/Cellar/go/1.13.3/libexec/src/encoding/csv/reader.go:266 +0x115
encoding/csv.(*Reader).ReadAll(0xc00015c1b0, 0xc0005af2c0, 0x1000, 0xc0006fc000, 0xc0001da608, 0x0)
/usr/local/Cellar/go/1.13.3/libexec/src/encoding/csv/reader.go:202 +0x74
main.savePending(0xc00015c1b0, 0x0, 0x0, 0x0)
可能是什么问题?当启动
savePending
功能时,主例程继续从读卡器读取数据
在您同时使用的
csv.Reader
类型上中止for循环如何(从主线程和goroutine)。它的方法是线程安全的吗?啊,正确,认为它不是线程安全的
panic: runtime error: slice bounds out of range [:7887] with capacity 4096
goroutine 82 [running]:
bufio.(*Reader).ReadSlice(0xc0000c2ea0, 0x105930a, 0x88, 0x90, 0xc00090cab0, 0x0, 0x0)
/usr/local/Cellar/go/1.13.3/libexec/src/bufio/bufio.go:334 +0x232
encoding/csv.(*Reader).readLine(0xc00015c1b0, 0x9, 0x9, 0xc00090cab0, 0xc00090f680, 0x20e)
/usr/local/Cellar/go/1.13.3/libexec/src/encoding/csv/reader.go:218 +0x49
encoding/csv.(*Reader).readRecord(0xc00015c1b0, 0x0, 0x0, 0x0, 0xc00090cab0, 0x9, 0x9, 0x0, 0x0)
/usr/local/Cellar/go/1.13.3/libexec/src/encoding/csv/reader.go:266 +0x115
encoding/csv.(*Reader).ReadAll(0xc00015c1b0, 0xc0005af2c0, 0x1000, 0xc0006fc000, 0xc0001da608, 0x0)
/usr/local/Cellar/go/1.13.3/libexec/src/encoding/csv/reader.go:202 +0x74
main.savePending(0xc00015c1b0, 0x0, 0x0, 0x0)