英文:
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 Scanner
s around, as the buffers cannot be released until the Scanner
s are discarded. But perhaps a few extra kilobytes of memory don't matter much in your case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论