Is it a good idea to use a list of *bufio.Scanner for files to be read later in golang?

huangapple go评论91阅读模式
英文:

Is it a good idea to use a list of *bufio.Scanner for files to be read later in golang?

问题

我有一个包含分隔文件的列表,我想在获取它们的路径后读取这些文件。我想知道是否可以将路径存储为一个 *bufio.Scanner 的列表,这样以后读取起来会更容易(而且代码也更清晰)?以下是一个快速示例:

func main(){
    scannerList := read(filenameList)
    dowork(scannerList)
}

func read(filenameList []string) (scannerList []*bufio.Scanner){
    for _, filename := range filenameList{
        op, _ := os.Open(filename)
        defer op.Close()
        scanner := bufio.NewScanner(op)
        scannerList = append(scannerList, scanner)
    }
    return
}

func dowork(scannerList []*bufio.Scanner){
    for _, scanner := range scannerList{
        for scanner.Scan(){
            //读取内容
        }
        //处理内容
    }
}

类似上面示例的代码可以编译通过,但我不知道这是否被推荐(或者是否有效)。有什么意见吗?谢谢!

英文:

I have a list of delimited files to be read after I obtained their path. Instead of saving path as a string, I'm wondering can I simply store a list of *bufio.Scanner so those will be much easier to be read later (and code will be cleaner too)? Here is a quick example:

func main(){
    scannerList := read(filenameList)
    dowork(scannerList)
}

func read(filenameList []string) (scannerList []*bufio.Scanner){
    for _, filename := range filenameList{
        op, _ := os.Open(filename)
        defer op.Close()
        scanner := bufio.NewScanner(op)
        scannerList = append(scannerList, scanner)
    }
    return
}

func dowork(scannerList []*bufio.Scanner){
    for _, scanner := range scannerList{
        for scanner.Scan(){
            //read stuff
        }
        //do stuff
    }
}

My code similar to above example compiles, but I don't know if this is recommended (or works). Any comments? Thanks!

答案1

得分: 1

一个Scanner是一个复杂的结构,它嵌入了一个缓冲区。缓冲区可以动态增长(取决于scan函数的请求),最多可以达到64kB(MaxScanTokenSize)。

因此,通常情况下,保留多余的Scanner并不是一个好主意,因为在Scanner被丢弃之前,缓冲区无法释放。但是在你的情况下,也许多出来的几千字节的内存并不重要。

英文:

A Scanner is a complicated structure, and one that embeds a buffer. The buffer can grow dynamically (depending on what the scan function requests) up to 64kB (MaxScanTokenSize).

So in general it is not a good idea to keep redundant Scanners around, as the buffers cannot be released until the Scanners are discarded. But perhaps a few extra kilobytes of memory don't matter much in your case.

huangapple
  • 本文由 发表于 2017年7月12日 07:39:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/45046093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定