1 回答
TA贡献1943条经验 获得超7个赞
该问题似乎是由于 2 个文件扫描仪同时移动头部所致。
可以通过创建 2 个文件句柄(每个句柄都有自己的扫描仪)来实现所需的结果。以下对我有用
package main
import (
"fmt"
"log"
"os"
"text/scanner"
"time"
)
func main(){
var file [2]*os.File
var err error
file[0], err = os.Open("./abc.txt")
file[1], err = os.Open("./abc.txt")
if err != nil {
log.Panic(err)
}
defer file[0].Close()
defer file[1].Close()
var scanner [2]scanner.Scanner
inChan := make(chan string)
for i := 0; i < 2; i++ {
var n int64 = (int64)(i) * 26
file[i].Seek(n, 0)
scanner[i].Init(file[i])
fmt.Println(scanner[0].Pos)
go parseLine(fmt.Sprintf("Scanner %v:", i), &scanner[i], inChan)
}
for msg := range inChan {
fmt.Println(msg)
}
}
func parseLine(name string, scanner *scanner.Scanner, out chan string) {
for i := 0; i < 26; i++ {
out <- fmt.Sprintf("%s %c", name, scanner.Next())
}
time.Sleep(time.Second * 10)
close(out)
}
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报