New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: encoding/csv: add Buffered #51539
Comments
Please follow the instruction/template of proposal. |
Is I note that |
@ianlancetaylor Here is an examples
|
OK, if you don't want If performance is an issue, then consider that anything we do is going to cost a little bit of performance, only it will cost that performance for everybody. But I don't see why performance would be a significant issue here. |
@ianlancetaylor Is it possible to add the following functions?
Therefore, it is possible to know the number of bytes that were actually read. |
|
Adding a Can we take a step back? What is the problem that you are trying to address? |
OK, it's make sense. I want to calculate the percentage of bytes that have been read from the file. Due to the buffer of How about check the type of Or others. |
That already happens. See the implementation of |
Thanks. It's work. package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"os"
)
type ProgressReader struct {
r io.Reader
totalBytes int64
bytes int64
}
func NewProgressReader(totalBytes int64, r io.Reader) *ProgressReader {
if totalBytes <= 0 {
panic("the totalBytes must greater than 0")
}
return &ProgressReader{
r: r,
totalBytes: totalBytes,
}
}
func (r *ProgressReader) Read(p []byte) (n int, err error) {
n, err = r.r.Read(p)
r.bytes += int64(n)
return n, err
}
func (r *ProgressReader) Percentage() float64 {
return 100 * float64(r.bytes) / float64(r.totalBytes)
}
func (r *ProgressReader) PercentageOffset(n int64) float64 {
return 100 * float64(r.bytes+n) / float64(r.totalBytes)
}
func (r *ProgressReader) Bytes() int64 {
return r.bytes
}
func main() {
f, err := os.Open("xxx.csv")
if err != nil {
panic(err)
}
stat, err := f.Stat()
if err != nil {
panic(err)
}
totalBytes := stat.Size()
pr := NewProgressReader(totalBytes, f)
br := bufio.NewReader(pr)
r := csv.NewReader(br)
for {
_, err := r.Read()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
fmt.Printf(
"%.3f %.3f %d\n",
pr.Percentage(),
pr.PercentageOffset(int64(-br.Buffered())),
-br.Buffered(),
)
}
} |
OK, should we consider this proposal to be retracted? Thanks. |
OK, thanks again! |
When calculating the progress of reading, since
*bufio.Reader
have buffer, it cannot be calculated correctly.The text was updated successfully, but these errors were encountered: